thormayer
thormayer

Reputation: 1070

JQuery code to long tap for long term event

Im trying to fire an event of "long tap", so if the user wont raise his finger of the mouse the code will continue adding "yay!" forever:

this is my code :

var pressTimer

            $("#area").mouseup(function () {
                clearTimeout(pressTimer)
                // Clear timeout
                return false;
            }).mousedown(function(){
                // Set timeout
                pressTimer = window.setTimeout(function () {
                    $('#result').html('yay!');
                }, 1000)
                return false; 
            });

so far, its only firing it once, and then stops. what can I do to make it happened?

Upvotes: 0

Views: 51

Answers (3)

Barbara Laird
Barbara Laird

Reputation: 12717

You need to setInterval instead of setTimeout, and you need to append to the text of your result div instead of replacing it's content. There are multiple ways to append, but I just did a string concat in the example. http://jsfiddle.net/7E7Jk/

var pressTimer;
var $result = $('#result');

$("#area").mouseup(function () {
    clearInterval(pressTimer)
    // Clear timeout
    return false;
}).mousedown(function () {
    // Set timeout
    pressTimer = window.setInterval(function () {
        $result.text($result.text() + 'yay!');
    }, 1000)
    return false;
});

Upvotes: 1

Paulo Lima
Paulo Lima

Reputation: 1238

I believe that it is seeking

http://jsfiddle.net/Ze3FH/

var pressTimer;
        $("#area").mouseup(function () {
            clearInterval(pressTimer)
            // Clear timeout
            return false;
        }).mousedown(function(){
            // Set timeout
            pressTimer = window.setInterval(function () {
                $('#result').html($('#result').html() + 'yay!');
                alert("2");
            }, 1000);                
            return false; 
        });

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

Use setInterval inside your setTimeout and use clearInterval to stop.

Upvotes: 1

Related Questions