Javascript setInterval使用按钮启动或单独启动
如何使用按钮激活/触发/触发设置间隔?如果没有点击按钮,如何使它在3秒钟内自动启动,但是如果我点击按钮,它会在0.1秒启动 第二步,重启每件事。请帮帮我,伙计们Javascript setInterval使用按钮启动或单独启动,javascript,jquery,Javascript,Jquery,如何使用按钮激活/触发/触发设置间隔?如果没有点击按钮,如何使它在3秒钟内自动启动,但是如果我点击按钮,它会在0.1秒启动 第二步,重启每件事。请帮帮我,伙计们 $('button').click(function(){ setInterval(function(){ updateStats("updateStats"); }, 100); }); else setInterval(function(){ updateStats("updateS
$('button').click(function(){
setInterval(function(){
updateStats("updateStats");
}, 100);
});
else
setInterval(function(){
updateStats("updateStats");
}, 3000);
编辑
只有每次点击按钮100毫秒,如果没有点击,则大多数按钮会在3000毫秒自动点击,除非您在ofc中点击
第部分:
function updateStats(stat)
{
var stat = ["GAME","USERS"];
var url = "NET.php";
$.each(stat, function(i, key){
$.post(url, {stats: key}, function(data) { // stats to stat
$("#" + key).html(data);
$('.s').emoticonize({
//delay: 800,
//animate: false
});
});
});
}
将其保存在全局变量中,以在单击时清除超时引用:
var tid = setTimeout(function(){
updateStats("updateStats");
}, 3000);
$('button').click(function(){
clearTimeout(tid);
setInterval(function(){
updateStats("updateStats");
}, 100);
});
将其保存在全局变量中,以在单击时清除超时引用:
var tid = setTimeout(function(){
updateStats("updateStats");
}, 3000);
$('button').click(function(){
clearTimeout(tid);
setInterval(function(){
updateStats("updateStats");
}, 100);
});
页面加载调用setInterval在3秒后触发 单击按钮,在100毫秒内调用setInterval,并清除默认间隔
$('button').click(function(){
setInterval(function(){
updateStats("updateStats");
clearInterval(defaultTimer);
}, 100);
});
var defaultTimer = setInterval(function(){
updateStats("updateStats");
}, 3000);
如果不想重复此操作,请使用setTimeout而不是setInterval。在页面加载时,在3秒钟后调用setInterval以触发 单击按钮,在100毫秒内调用setInterval,并清除默认间隔
$('button').click(function(){
setInterval(function(){
updateStats("updateStats");
clearInterval(defaultTimer);
}, 100);
});
var defaultTimer = setInterval(function(){
updateStats("updateStats");
}, 3000);
如果不想重复此操作,请使用setTimeout而不是setInterval。如果单击按钮,此操作将在3000后触发,它将在100后触发
var x ;
// call timer after 3000
callTimer(3000);
// call timer after 100 if cicked
$('button').click(function(){
clearInterval(x);
callTimer(100);
});
function callTimer(time){
x = setInterval(function(){
updateStats("updateStats");
}, time);
}
根据您的要求更新
var x ;
var y ;
// call timer after 3000
callTimer(3000);
// call timer after 100 if cicked
$('button').click(function(){
y = setInterval(function(){
updateStats("updateStats");
clearInterval(y);
}, 100);
callTimer(3000);
});
function callTimer(time){
clearInterval(x);
x = setInterval(function(){
updateStats("updateStats");
}, time);
}
这将在3000后触发,如果单击按钮,它将在100后触发
var x ;
// call timer after 3000
callTimer(3000);
// call timer after 100 if cicked
$('button').click(function(){
clearInterval(x);
callTimer(100);
});
function callTimer(time){
x = setInterval(function(){
updateStats("updateStats");
}, time);
}
根据您的要求更新
var x ;
var y ;
// call timer after 3000
callTimer(3000);
// call timer after 100 if cicked
$('button').click(function(){
y = setInterval(function(){
updateStats("updateStats");
clearInterval(y);
}, 100);
callTimer(3000);
});
function callTimer(time){
clearInterval(x);
x = setInterval(function(){
updateStats("updateStats");
}, time);
}
$(“按钮”)。单击(函数(){
clearInterval(interval);
setInterval(函数(){
updateStats(“updateStats”);
},100);})代码>
$(“按钮”)。单击(函数(){
clearInterval(interval);
setInterval(函数(){
updateStats(“updateStats”);
},100);})代码>您希望它每100毫秒发射一次,还是在按钮点击100毫秒后仅发射一次?请具体说明。“如果按钮X被点击到这个位置,那么其他位置”的逻辑有点奇怪。单击按钮是一种操作,而不是一种状态,因此在if-else上下文中没有真正意义。抱歉,只有每次单击按钮100毫秒,如果没有单击,则大多数按钮会在3000毫秒时自动启动,除非您单击它,否则您希望它每100毫秒启动一次,还是在单击按钮100毫秒后仅启动一次?请更具体。“如果按钮X被点击到这个位置,那么其他位置”的逻辑有点奇怪。点击按钮是一种行为,而不是一种状态,因此在if-else上下文中没有意义。抱歉,每次点击按钮100毫秒,如果没有点击,则大多数按钮会在3000毫秒时自动启动,除非你点击它,否则他说它只会在评论中触发一次,因此,将setInterval
更改为setTimeout
,就得到了+1;)是的,如果OP编辑了原始问题,而不是添加了另一条有重要要求的注释,那么就更容易理解了……我试过了,但是按钮需要更长的时间,超过100毫秒才能知道。他说它应该只在注释中触发一次,所以将setInterval
更改为setTimeout
,您得到了+1;)是的,如果OP编辑了原始问题,而不是添加了另一条有重要要求的评论,那么就更容易理解了…我试过了,但是按钮需要更长的时间,超过100毫秒才能知道。如果按钮被点击,你能重设3000毫秒吗?我试过点击按钮,看起来像是等待更长时间我试过了,但是当我点击。。它工作不快。。它的行为就好像点击是关于你添加的代码。。。您发送的post请求可能会占用一段时间,因此您可能会觉得它无法快速工作。这是真的,如何不让它占用该时间它必须占用一点时间,因为它是一个新请求。您发送的新请求就像一次到服务器端的返回,因此需要一段时间。如果单击按钮,您可以重置3000毫秒吗?,我试着点击按钮,看起来好像等了很久,我试过了,但是当我点击的时候。。它工作不快。。它的行为就好像点击是关于你添加的代码。。。您发送的post请求可能会占用一段时间,因此您可能会觉得它无法快速工作。这是真的,如何不让它占用该时间它必须占用一点时间,因为它是一个新请求。您发送新请求就像一次到服务器端的旅程,返回需要一段时间。我正在单击按钮,但花了100多英镑ms@user1417815因为您使用的是(ajax)$.post
,所以不能强制在100中加载它ms@user1417815没有什么诀窍,您必须等待ajax请求ends@David查看他正在使用的更新问题$。在updateStats()中发布,但花了100多英镑ms@user1417815因为您使用的是(ajax)$.post
,所以不能强制在100中加载它ms@user1417815没有什么诀窍,您必须等待ajax请求ends@David查看他正在使用的更新问题$。在updateStats()中发布按钮超过100毫秒多少?这不需要太长时间。如何测量它需要更长的时间?如果您有firebug,请尝试使用console.log。它可以给你一些调试信息。按钮超过100毫秒还有多少?这不需要太长时间。如何测量它需要更长的时间?如果您有firebug,请尝试使用console.log。它可以为您提供一些调试信息。