清除一个超时,开始另一个超时
我使用 ajax/json 自动刷新网站上的内容。使用 jkey,我触发了对文档的一些发布,在此操作期间,我想取消正在运行的原始 setTimeout(在 2 分钟时“重新加载”),然后再次触发整个函数“内容”以在 5 秒后重新开始。但是,我似乎无法正确停止“重新加载”,也无法在给定的秒数后调用“内容”。有人能发现错误吗?
<script>
$(function content(){
function reloading(){
$.ajax({
url: 'api.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
_id = id;
var vname = data[1];
var message = data[2];
var timestamp = data[3];
var field1 = data[4];
_field1 = field1;
var val2 = parseInt(field1, 10) ;
_val2 = val2;
$('#output').hide().html( message ).fadeIn("slow");
$('#username').hide().html( vname +":" ).fadeIn("slow");
setTimeout(reloading,120000);
}
});
}
reloading();
});
$(document).jkey('a',function() {
$.post("update.php", { "id": _id} )
$('#output').hide().html( "<i>thx" ).fadeIn("slow");
$('#username').fadeOut("fast");
$('#valg1').fadeOut("fast");
$('#valg2').fadeOut("fast");
clearTimeout(reloading);
setTimeout(content,5000);
});
</script>
I'm auto-refreshing the content on a site using ajax/json. Using jkey, I trigger some posting to the document, and during this action I want to cancel the original setTimeout that's running ("reloading" at 2 mins) - and then trigger the entire function "content" again to start over after 5 secs. However, I can't seem to stop "reloading" properly, neither call "content" after the given seconds. Can anybody spot the error?
<script>
$(function content(){
function reloading(){
$.ajax({
url: 'api.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
_id = id;
var vname = data[1];
var message = data[2];
var timestamp = data[3];
var field1 = data[4];
_field1 = field1;
var val2 = parseInt(field1, 10) ;
_val2 = val2;
$('#output').hide().html( message ).fadeIn("slow");
$('#username').hide().html( vname +":" ).fadeIn("slow");
setTimeout(reloading,120000);
}
});
}
reloading();
});
$(document).jkey('a',function() {
$.post("update.php", { "id": _id} )
$('#output').hide().html( "<i>thx" ).fadeIn("slow");
$('#username').fadeOut("fast");
$('#valg1').fadeOut("fast");
$('#valg2').fadeOut("fast");
clearTimeout(reloading);
setTimeout(content,5000);
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
clearTimeout
应该获取由setTimeout
返回的唯一“key”。所以在设置时,将返回值赋给全局变量:然后有这样的:
The
clearTimeout
should get the unique "key" that is returned bysetTimeout
. So when setting, assign the return value to global variable:Then have this:
您必须保存
setTimeout()
ID,以便稍后使用clearTimeout()
清除它。像这样:You must save the
setTimeout()
id in order to later clear it withclearTimeout()
. Like this:除了保存其他帖子中提到的超时 ID 之外,您的
函数重新加载
是在函数内容
内创建的,并且由于您没有创建它的闭包,因此程序的其余部分无法访问它。你必须做这样的事情:
如果不了解大局,要写得更好有点困难。这样,
content
将在 5 秒后被调用,只要reloading
成功,它就会每 120 秒回调一次。请注意,重新加载
永远不会以这种方式清除。Besides saving the timeout id as mentioned in other posts, your
function reloading
is created insidefunction content
and since you create no closure to it, it's unreachable from the rest of the program.You have to do something like this:
It's kinda difficult writing this better not knowing the bigger picture. With this,
content
will be called after 5 seconds and as long asreloading
succeeds it will callback itself every 120 seconds. Please observe thatreloading
is never cleared this way.