INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Disable page refresh javascript on click

Share

Disable page refresh javascript on click

Disable page refresh javascript on click

(OP)
I have a javascript that refreshes my page every 45 seconds. I want to disable the page refresh if the user is filling a form on the page.

Otherwise in IE i lose all data. I want the user to be able to enable the javascript again. Is this possible?

A button that could be clicked to disable the refresh and then enable it again.


Here is my javascript code

CODE


<script>
<!--

//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit="0:45"

if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh(){
if (!document.images)
return
if (parselimit==1)
window.location.reload()
else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
else
curtime=cursec+" seconds left until page refresh!"
window.status=curtime
setTimeout("beginrefresh()",1000)
}
}

window.onload=beginrefresh

</script>

RE: Disable page refresh javascript on click

How will you know if a user is filling out the form on the page?

You can check by seeing if any form fields are containing non default values, but there could be a better way, and I don't have enough info to know if there is.

monkeysnake <.

RE: Disable page refresh javascript on click

(OP)
I dont want to know if they are filling out a form. If they click a button it disables the refresh.

So before they start filling out the form they will click the button to disable the refresh.

RE: Disable page refresh javascript on click

Hi

CODE

...

window.status=curtime
var refreshTimer=setTimeout("beginrefresh()",1000)
}
}

window.onload=beginrefresh

</script>
</head>
<body>
<form action="#">
<input type="button" value="Start" onclick="beginrefresh()">
<input type="button" value="Stop" onclick="clearTimeout(refreshTimer)">
</form>

...

Feherke.
http://rootshell.be/~feherke/

RE: Disable page refresh javascript on click

Got ya.

You probably need to do this, since you can have a setTimeout going when the disable button is clicked

Make a global variable (disableRefresh).
On the click of a button, make toggle the value of disableRefresh (true/false)

Check in beginrefresh for the value of disableRefresh.

CODE

var disableRefresh = false;

function beginrefresh(){
if (!disableRefresh) {
   if (!document.images)
      return
   if (parselimit==1)
      window.location.reload()
   else{
      parselimit-=1
      curmin=Math.floor(parselimit/60)
      cursec=parselimit%60
      if (curmin!=0)
         curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
      else
         curtime=cursec+" seconds left until page refresh!"
      window.status=curtime
      setTimeout("beginrefresh()",1000)
   }
}
}


//HTML
<input type="button" value="Disable Auto-Refresh" onclick="disableRefresh = !disableRefresh" />

monkeysnake <.

RE: Disable page refresh javascript on click

(OP)
Thanks.

But I keep getting this error - refreshTimer is not defined

I know the var is defined.

Am I doing something wrong?

RE: Disable page refresh javascript on click

If you're defining it with var inside the function, that variable won't exist outside the function.

monkeysnake <.

RE: Disable page refresh javascript on click

(OP)
Thank you so much guys it worked!

RE: Disable page refresh javascript on click

Hi

Yep. I am used to work with indented code, which is incomparably more readable then countdrak's.

Feherke.
http://rootshell.be/~feherke/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close