我有一个倒计时运行和点击按钮我想停止这个倒计时.但它不工作.
这是我的代码
$(document).ready(function (){
var settimmer =1000;
var $dash_url="<?php echo DASH_URL;?>";
$(function (){
var $interval=setInterval(function() {
var timeCounter = settimmer;
var updateTime = eval(timeCounter)- eval(1);
if(updateTime => 0){
settimmer=updateTime;
$('.redirect').css({"color":"#FFF"});
$(".redirect").html('Redirecting You in '+settimmer+' Second');
}
if(updateTime <= 0){
clearInterval($interval);
window.location.replace($dash_url);
return false;
}
},1000);
});
$('.stop_timer').click(function (){
clearInterval($interval);
});
});
解决方法:
这会将$interval变量放在正确的范围内.
$(document).ready(function (){
var settimmer =1000;
var $dash_url="<?php echo DASH_URL;?>";
$(function (){
var $interval=setInterval(function() {
var timeCounter = settimmer;
var updateTime = eval(timeCounter)- eval(1);
if(updateTime => 0){
settimmer=updateTime;
$('.redirect').css({"color":"#FFF"});
$(".redirect").html('Redirecting You in '+settimmer+' Second');
}
if(updateTime <= 0){
clearInterval($interval);
window.location.replace($dash_url);
return false;
}
},1000);
$('.stop_timer').click(function (){
clearInterval($interval);
});
});
});