Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable page refresh javascript on click

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
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>
 
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.

[monkey][snake] <.
 
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.
 
Hi

Code:
[gray]...[/gray]

window.status=curtime
[red]var refreshTimer=[/red]setTimeout("beginrefresh()",1000)
}
}

window.onload=beginrefresh

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

[gray]...[/gray]

Feherke.
 
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" />


[monkey][snake] <.
 
Thanks.

But I keep getting this error - refreshTimer is not defined

I know the var is defined.

Am I doing something wrong?
 
If you're defining it with var inside the function, that variable won't exist outside the function.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top