2013-02-25 64 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); 
}); 

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