Echo89

setInterval and setTimeout example.

Jun 6th, 2013
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // To execute a function every two seconds, you do this:
  2.  
  3. var interval = setInterval(function() { alert("Hello"); }, 2000);
  4.  
  5. // Then to clear the interval (I.E. Stop it) you just do this:
  6.  
  7. clearInterval(interval);
  8.  
  9. // And to execute a function once after a certain amount of time, you simply use this instead:
  10.  
  11. var timeout = setTimeout(function() { alert("Hello"); }, 2000);
  12.  
  13. // And to clear the timeout (I.E. Stop it before executing the function) you do this:
  14.  
  15. clearTimeout(timeout);
RAW Paste Data