无法清除我的setTimeout

时间:2012-10-17 01:02:13

标签: javascript

var timer;
var object;
var thing;
var digit="0";

//this function fires when a single click occurs

function one(event){
   timer = setTimeout("AddDigit(object)",500);
   object = event;
}

//The AddDigit function keeps firing right after my double click function 
function AddDigit(x){
   object=x;
   if (eval(digit) == 0){ digit = object; }
   else{ digit = digit + object; }

   document.calculator.display.value = object;

}
// This function is supposed to stop the AddDigit function from firing...

document.ondblclick = function(button){
   clearTimeout(timer);

   thing=button.target;
   thing.setAttribute("class","duction");
}

1 个答案:

答案 0 :(得分:1)

双击调度两个点击事件,然后双击事件。第二次调用one时,它会用新ID替换timer,但原始超时仍然准备好了。当调用dblclick处理程序时,它会清除第二个超时,但不会清除第一个超时。

解决方案是在分配one之前清除timer中的所有现有超时。

function one(event)
{
    clearTimeout(timer);
    timer = setTimeout(function() {
        AddDigit(event);
    }, 500);
}

请参阅http://jsfiddle.net/74qYF/