jquery - How do I stop a window.setInterval in javascript? -


I have a javascript function called every 2000ms, I want to stop it, so I can tell the user other things on the page Without it it can be called again. Is this possible? This function is called every 2,000 mms:

  window.setInterval (function getScreen (sid) {if (window.XMLHttpRequest) {// code for IE7 +, Firefox, Chrome, Opera , Safari Xmlhttp = new XMLHttpRequest ();} else {// code for IE6, IE5 xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange = function () {if (xmlhttp.readyState == 4 & amp; ; Xmlhttp.status == 200) {document.getElementById ("Refresh"). InnerHTML = xmlhttp.responseText;}} xmlhttp.open ("Post", "getScreen.php? Sid =" + & lt ;? php echo $ Sid;? & Gt ;, true); xmlhttp.send ();}, 2000); There is no built-in "stop" function in it, but you can stop it, And then start a new interval with the same function.  

First of all, you need to capture the returned ID by calling setInterval :

  var interval = window.setInterval (... );   

Then when you want to stop it, call

  window.clearInterval (intervalId);   

In your case, I recommend defining setScreen , and not inside the call for setInterval . In this way you can simply use intervalId = window.setInterval (setScreen, 2000) when you want to start it again.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

ruby on rails - Convert a Activerecord result with has_one associations into a single hash per record -

javascript - Read complete data from stream from onMessage -