当MouseDown,首先慢慢减少数量,然后增加下降速度(jQuery)

时间:2014-06-19 13:13:41

标签: javascript jquery html timer mousedown

标题显示我遇到了MouseDown问题。 我想要的“伪代码”

While ("#arrowUp").mouseDown(function() {
    counter++; //One time directly when pressed
    if(MouseDownTime > 500){ //500ms that is
       setTimeOut({counter++}, 75);  //Meaning, every 75ms counter++
    }

}

我已经在Stack Overflow上看了两天多了。我成功地每75ms递增一次,但后来我无法构建if(MouseDownTime > 500)语句,同时仍能在500ms后每隔75ms增加一次计数器。

$("#tempUp").mousedown(function() {     //When longer pressed, automatically faster increase Temperature
    int = setInterval(editTemp(currTemp+1), 250);
    })
                .mouseup(function() {
    clearInterval(int);
    numberOfRepeats = 0;        
    }); 

到目前为止,这是我的功能代码。任何人都可以帮我吗?或者我是以错误的方式提问? (非建设性)

1 个答案:

答案 0 :(得分:7)

如果我理解正确,您可以使用setTimeoutsetInterval的组合,如下所示:

$(document).ready(function () 
{
    var temp = 0;
    var to   = null;
    var iv   = null;

    $("#ClickMe").on("mousedown", function () 
    {
        temp++;
        $("#Temp").html(temp);
        to = setTimeout(function () 
        {
            iv = setInterval(function () 
            {
                temp++;
                $("#Temp").html(temp);
            }, 75);
        }, 500);
    }).on("mouseup mouseleave", function () 
    {
        clearTimeout(to);
        clearInterval(iv);
    });
});

有关示例,请参阅此 FIDDLE

编辑:添加了mouseleave事件以及JoséF。Romaniello的建议。