2013-02-25 66 views
0

我有以下代碼我如何每n秒運行一次jQuery load()請求?

$('document').ready(function() { 
    reload(); 
}); 

function reload() { 
    $('div#info').load('http://somesite.ru/script.php'); 
    setInterval(reload(), 10000); 
} 

但好像方法重載()速度過快。 Firefox顯示我的消息,關於jquery.min.js正在繁忙。我如何每10秒鐘刷新一次頁面的一部分?

+0

無限循環:D – Alexander 2013-02-25 08:54:39

+0

http://stackoverflow.com/questions/10425107/why-isnt-clearinterval-working-in-this-code – undefined 2013-02-25 09:06:38

回答

2

您應該刪除(),並將setInterval函數放在reload函數的上下文之外。

function reload() { 
    $('#info').load('http://somesite.ru/script.php'); 
} 
$(document).ready(function() { 
    setInterval(reload, 10000); 
}); 
1

替換:

setInterval(reload(), 10000); 

有:

setInterval(reload, 10000); 
1

使用的setTimeout(),而不是它是安全和性能方面比對的setInterval()很好的滿足您的要求。

var time= setTimeout(reload,1000); 

後的重裝()方法檢查一定條件下調用它裏面的setTimeout再次

function reload() 
{ 
/// your logic 
setTimeout(reload,1000); 
} 

使用上述變量摧毀間隔,只要你不想重裝了

clearTimout(time); 
0
 Refer: http://www.w3schools.com/js/tryit.asp?filename=tryjs_setinterval 

    setInterval(function(){reload();},10000); 
0

另一種方式是隻使用

$('document').ready(function() { 
    setInterval(function() { 
    $('div#info').load('http://somesite.ru/script.php'); 
    }, 10000); 
}); 

所有工作正常。非常感謝你的回答。