Skip to content Skip to sidebar Skip to footer

Delay Mousedown Interval Start (JQuery / Javascript)

I am writing a jQuery plugin that manipulates the value of an input field at the press of a button. What I have so far is the ability to control the value by clicking the button,

Solution 1:

Wrap the setInterval in a setTimeout. And, like interval, keep and clear a timeout value:

var interval, timeout;

// ...

    timeout = window.setTimeout(function () {
        interval = window.setInterval(function() {
            element.val(parseInt(element.val()) + 1);
        }, 200);      
    }, 400);

// ...

    window.clearTimeout(timeout);
    window.clearInterval(interval);

http://jsfiddle.net/coiscir/jUSg8/


Solution 2:


Solution 3:

just use setTimeout to delay setInterval

mousedown: function(e) {
    setTimeout(function() {
        interval = window.setInterval(function() {
            element.val(parseInt(element.val()) + 1);
        }, 200);
    }, 400);
    e.preventDefault();
},
mouseup: function() {
    window.clearTimeout(timeout);
    window.clearInterval(interval);
}

Post a Comment for "Delay Mousedown Interval Start (JQuery / Javascript)"