首页 > 应用开发 > > if做判断执行到第三次就不执行了?
$(function(){
vartimer = setInterval(function(){run()},400);
varonOff = true;
$('button').click(function(){
if(onOff){
clearInterval(timer);
$(this).html('开始');
onOff = false;
//console.log(onOff)
}else{
setInterval(function(){run()},400);
$(this).html('停止');
onOff = true;
//console.log(onOff)
}
});
functionrun(){console.log(Math.random())}
})
1<button>停止</button>
想要做个点击按钮停止输出随机数,再按按钮开始输出随机数,再按停止,再按开始……的功能。可是按了第三次就无效了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1347/viewspace-3128345/ ,如需转载,请注明出处,否则将追究法律责任。