settimeout 怎么取消?
比如
function f2()
{
alert( '测试 ');
}
function f1()
{
var time = window.setTimeout( "f2() ",2000);
}
</script>
<input type= "text " onclick= "f1() "/>
如果我在2秒内连续点击,点击多少次就会执行多少次
我想,如果在2秒内连续点击,让程序停止前面的记数,只执行最后一次
------解决方案--------------------var running = false;
function f2()
{
alert( '测试 ');
running = false;
}
function f1()
{
if(running == true){
return;
}
running = true;
var time = window.setTimeout( "f2() ",2000);
}
------解决方案--------------------clearTimeout(time);
------解决方案--------------------var time;
function f2()
{
alert( '测试 ');
}
function f1()
{
time = window.setTimeout( "f2() ",2000);
clearTimeout(time);
}
</script>
<input type= "text " onclick= "f1() "/>