开发者

Using timers (onload)

开发者 https://www.devze.com 2023-02-19 20:20 出处:网络
Ok, firstly, I hardly know Javascript. I really don\'t know what I\'m doing. So, I have this code: var interval_id = 0;

Ok, firstly, I hardly know Javascript. I really don't know what I'm doing. So, I have this code:

var interval_id = 0;
var prevent_bust = 0;


 // Event handler to catch execution of the busting script.
 window.onbeforeunload = function() { prevent_bust++ };

 // Continuously monitor whether busting script has fired.
 interval_id = setInterval(function() {
   if (prevent_bust > 0) {  // Yes: it has fired. 
     prevent_bust -= 2;     // Avoid further action.
     // Get a 'No Content' status which keeps us on the same page.
     window.top.location = 'http://vadremix.com/204.php';
   }
 }, 1);

   function clear ()
   {
       clearInterval(interval_id);
   }

   window.onload="setTimeout(clear (), 1000)";

After 1 second I want to clear the interval set earlier. This isn't working. How would I do what I开发者_JS百科'm trying to do?


If you substitute the last line with window.onload = function() { setTimeout(clear, 1000); }, it should do OK.

There are two errors in your code:

  • window.onload should be a function, rather than a string ("..."),
  • setTimeout accepts a function (clear), rather than the result from the function (clear())

By the way, these are some good places to learn JavaScript:

  • QuirksMode
  • Mozilla Developer Network
0

精彩评论

暂无评论...
验证码 换一张
取 消