0
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");
}
4

1 回答 1

1

双击分派两个单击事件,然后是双击事件。第二次one被调用,它timer用一个新的 id 代替,但原来的超时时间仍然准备好了。当 dblclick 处理程序被调用时,它会清除第二次超时,但不会清除第一次超时。

one解决方案是在分配之前清除任何现有的超时timer

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

http://jsfiddle.net/74qYF/

于 2012-10-17T01:26:03.717 回答