最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论