2011-03-27 59 views
2

好吧,首先,我几乎不知道Javascript。我真的不知道我在做什么。 所以,我有这样的代码:第二使用定时器(onload)

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)"; 

后1我要清除的时间间隔先前设置。这不起作用。我将如何做我想做的事?

回答

1

如果用window.onload = function() { setTimeout(clear, 1000); }代替最后一行,它应该可以。

有两个错误在你的代码:

  • window.onload应该是一个function,而不是字符串("..."
  • setTimeout接受函数(clear),而不是从函数的结果(clear()

顺便说一句,这些都是一些好的地方学习JavaScript:

+0

谢谢!另外,感谢这些链接,显然,我一直有意要学习JavaScript一段时间,但从来没有想到 – James 2011-03-27 08:50:09