Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 1.70 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. javascript clearTimeout not clearing timeout!.. this shouldn't be hard?
  2. var t = 0;
  3.  
  4. function check_status() {
  5. var time_now = Math.floor(new Date().getTime() / 1000);
  6. var last_activity = document.getElementById("last_activity").value;
  7. var since_last_activity = time_now-last_activity;
  8. console.info(since_last_activity);
  9. if(since_last_activity >= 20)
  10. {
  11.     // show div
  12.     document.getElementById("logout_warning").style.height = document.documentElement.clientHeight+"px";
  13.     document.getElementById("logout_warning").style.display = 'block';
  14.     // start countdown
  15.     var t = setTimeout("logout();", 10000);
  16. }
  17. }
  18.  
  19. function logout() {
  20. document.getElementById("logout_warning").style.display = 'none';
  21. location.href="/user/logout";
  22. }
  23.  
  24. function renew() {
  25. clearTimeout(t);
  26. var time_now = Math.floor(new Date().getTime() / 1000);
  27. document.getElementById("last_activity").value = time_now;
  28. document.getElementById("logout_warning").style.display = 'none';
  29. }
  30.  
  31. setInterval('check_status()',10000);
  32.        
  33. <div id="logout_warning" style="display:none; width:100%; height:500px; top:0px; left:0px; position:absolute; background-image:url('/images/overlay.png'); z-index:100000;">
  34. <div style="width:300px; position:relative; margin:200px auto; border:1px solid #000000; background-color:#FFFFFF; padding:10px; text-align:center;">
  35. You're going to be logged out in 10 seconds! oh no!<br/><br/>
  36. <button type="button" onclick="renew();">Click here</button> to renew your session
  37. </div>
  38.        
  39. var t = 0;
  40.        
  41. var t = setTimeout("logout();", 10000);
  42.        
  43. clearTimeout(t);
  44.        
  45. var t = setTimeout("logout();", 10000);
  46.        
  47. var t = 0;
  48. function check_status() {
  49.    var t = setTimeout("logout();", 10000);
  50. // ^^^^
  51. }
  52.  
  53. function renew() {
  54.     clearTimeout(t);
  55. }