如何在jquery中正确删除计时器?
如果用户单击div1 close div将显示,我希望在用户单击close div时清除并删除计时器,并在用户单击div2时调用new timer(),但再次调用时newTimer()无法运行,并且jquery停止所有代码,那么如何正确清除和删除计时器 HTML 此外,此处还提供了测试代码 多谢各位 在这种情况下不应使用此关键字如何在jquery中正确删除计时器?,jquery,timer,clearinterval,Jquery,Timer,Clearinterval,如果用户单击div1 close div将显示,我希望在用户单击close div时清除并删除计时器,并在用户单击div2时调用new timer(),但再次调用时newTimer()无法运行,并且jquery停止所有代码,那么如何正确清除和删除计时器 HTML 此外,此处还提供了测试代码 多谢各位 在这种情况下不应使用此关键字new,因为new用于从函数返回对象实例,这也称为伪经典继承。此函数的结构方式并不表示您希望返回一个可以访问其属性和方法的对象 相反,只需执行timeris(newtim
new
,因为new
用于从函数返回对象实例,这也称为伪经典继承。此函数的结构方式并不表示您希望返回一个可以访问其属性和方法的对象
相反,只需执行
timeris(newtimetime,newTimerFunc)
即可获得所需的结果 请重新措辞你的问题,这是非常不可能阅读和理解!谢谢你,我改变了我的问题,我希望它很好,还是很难读懂。。稍微深入一点,你有许多缺少的分号(;
),我真的不明白你为什么使用你的代码,看来你的问题——我不完全理解——可以更容易地解决。当您单击#div1
、单击#showClose
添加#div2
时,是否确实要显示您的#showClose
?下一步该怎么办?此代码的目的是在单击“showClose”时清除计时器,停止第一个计时器,并在再次单击“div1”或“div2”时再次运行新计时器,但代码无法运行新计时器。我真的不确定您试图实现什么,但这是吗?
<div id="div1" style="width:140px; height:140px; line-height:140px; text-align:center; font-size:24px; background-color:rgba(51,153,204,.5);">
Click 1
</div>
<div id="div2" style="width:140px; height:140px; line-height:140px; text-align:center; font-size:24px; background-color:rgba(21,113,214,.5);">
Click 2
</div>
<div id="showClose" style="position: absolute; width: 440px; height: 140px; line-height: 140px; text-align: center; font-size: 24px; background-color: rgba(21,113,214,.5); left: 276px; top: 272px;">
Close Win
</div>
$("#showClose, #click2").hide()
var blnnewTimer = false;
var timeris = function myTimer(clockTime, inClockFunction) {
var stopClock = false;
var ChatClock = {
totalSeconds: 0,
start: function() {
this.interval = setInterval(function() {
if (!stopClock) {
stopClock = inClockFunction();
} else {
ChatClock.pause();
}
}, clockTime);
},
pause: function() {
clearInterval(this.interval);
delete this.interval;
}
};
ChatClock.start();
}
function newTimer() {
blnnewTimer = false;
var newTimerTime = 3000;
var newTimerFunc = function() {
alert(blnnewTimer)
return blnnewTimer;
}
newTimer = new timeris(newTimerTime, newTimerFunc);
}
$("#div1,#showClose,#div2").mouseover(function() {
$(this).css("cursor", "pointer");
$(this).css("color", "rgba(102,102,102,1)");
})
$("#div1,#showClose,#div2").mouseout(function() {
$(this).css("color", "rgba(0,0,0,1)");
})
$("#div1").click(function() {
newTimer()
$("#showClose").show(800)
})
$("#div2").click(function() {
newTimer() // here will not run newTimer function and code will be broken
$("#showClose").show(800)
})
$("#showClose").click(function() {
blnnewTimer = true;
$("#showClose").hide()
if (!$("#click2").is("visible")) $("#click2").show()
})
newTimer = new timeris(newTimerTime, newTimerFunc);