作为后续行动这个question的,我想知道这是为什么不工作clearTimeout和setTimeout的工作不
$("#textarea").keydown(function(){
oldValue = $("#textarea").val();
});
$("#textarea").keyup(function(){
var startTimer = null;
if(startTimer) clearTimeout(startTimer);
startTimer = setTimeout(function(){
var newValue = $("#textarea").val();
// get something out of the comparison
alert(something) // alert the comparison
oldValue = newValue;
},2000);
所需的行为将只能得到一个提示信息时,用户不是招” t键入任何东西2秒钟。它适用于比较部分,但是,当我继续打字时,它不会停止警报消息。相反,我得到的警报数量与按下的按键数量相同。 我试过这个创建和删除cookies的版本,它工作正常。这种特殊情况有什么不对?
var startTimer = null; if(startTimer)clearTimeout(startTimer); //条件永远不会是真的... – JAAulde 2011-03-07 18:02:58
@JAAulde:当然,你是对的。谢谢:-) – 2011-03-07 19:06:29