2011-03-27 76 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