Javascript 如何在我的情况下创建计时器?
我试着让我的按钮做两件事Javascript 如何在我的情况下创建计时器?,javascript,jquery,css,Javascript,Jquery,Css,我试着让我的按钮做两件事 初始化计时器以调用函数 调用相同的函数 我有如下的东西 test.prototype.setupEvent= function(){ var instance = this; $('#btn').on('click', function(){ clearInterval(instance.timer); this.showStuff() instance.timer=set
初始化计时器以调用函数 调用相同的函数 我有如下的东西
test.prototype.setupEvent= function(){
var instance = this;
$('#btn').on('click', function(){
clearInterval(instance.timer);
this.showStuff()
instance.timer=setInterval(function(){
instance.showStuff()
},10000);
})
}
test.prototype.showStuff= function(btnID){
//jump to another page
}
我的问题是,我希望用户在第一次单击按钮时能够在10秒后看到一些内容,但是,如果他们在10秒之前再次单击按钮,他们也可以看到内容。我不知道如何通过单击事件区分两种不同的状态。有人能帮我吗?谢谢 试试看
test.prototype.setupEvent = function () {
var instance = this;
$('#btn').on('click', function () {
//if there is a timer running then clear the timer, show the content and delete the timer reference
if (instance.timer) {
clearInterval(instance.timer);
instance.showStuff();
delete instance.timer
return;
}
//also you may want to use setTimeout() not setInverval()
instance.timer = setInterval(function () {
instance.showStuff();
delete instance.timer
}, 10000);
})
}
test.prototype.showStuff = function (btnID) {
//jump to another page
}
“init a timer to call a function call the same function”--请重写这一部分,我不知道你的意思,你能编辑来创建这个案例吗