javascript - While MouseDown, first slowly decrease number, then increase decreasing speed (jQuery) - Stack Overflow

As the title suggests I'm stuck with a MouseDown problem. What I want in "pseudocode"Wh

As the title suggests I'm stuck with a MouseDown problem. What I want in "pseudocode"

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

}

I have been looking around at Stack Overflow for over two days now. And I succeeded to increment every 75ms, but then I couldn't build in the if(MouseDownTime > 500)-statement, while still being able to increase the counter every 75ms after the 500ms.

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

This is code I have of of my function so far. Could anyone help me out? Or am I asking the question in a wrong way? (non-constructive)

As the title suggests I'm stuck with a MouseDown problem. What I want in "pseudocode"

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

}

I have been looking around at Stack Overflow for over two days now. And I succeeded to increment every 75ms, but then I couldn't build in the if(MouseDownTime > 500)-statement, while still being able to increase the counter every 75ms after the 500ms.

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

This is code I have of of my function so far. Could anyone help me out? Or am I asking the question in a wrong way? (non-constructive)

Share Improve this question asked Jun 19, 2014 at 13:13 Rich_RichRich_Rich 4743 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

If I understand you correctly you can make use of a bination of setTimeout and setInterval, like so:

$(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);
    });
});

See this FIDDLE for an example.

EDIT: Added the mouseleave event as well as suggested by José F. Romaniello.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745139896a4613381.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信