3

First i want to say that i am a newbie in the javascript world.

I want to write a function for a button:

when button is pushed -> x=0 ; post(x); display x;
when button is released -> x=1; post(x); display x;
when button is hold down -> while hold: 
            if(x=0){post(x); display x; x++}          
            if(x=1){post(x); display x; x--} 

This is what i have come up with so far:

http://pastebin.com/1myT891h

Help would be appreciated very much.

2
  • 3
    Move your code to jsfiddle.net or jsbin.com Commented Nov 29, 2012 at 15:05
  • 1
    What's post? And what do you mean by "while hold", an infinite loop? Commented Nov 29, 2012 at 15:09

3 Answers 3

2

Try this fiddle:

Html:

<button id="button">Click here</button><br/>
Status: <span id="status"></span>

Javascript:

observeTriState("#button", function(state) {
    var states = { '0':'Push', '1':'Release', '2':'Hold Down' };

    $("#status").text(states[state]);
}, 500);

function observeTriState(selector, callback, holdDelay) {
    var mouseDown = false;
    var mouseIn = false;
    var interval;

    function checkStatus() {
        if (mouseDown && mouseIn) {
            callback(2)
        }
    }

    $(selector).mousedown(function() {
        callback(0);
        mouseDown = true; 
        interval = setInterval(checkStatus, holdDelay); 
    }).mouseup(function() {
        mouseDown = false;
        callback(1);
        clearInterval(interval);
    }).mouseenter(function() {
        mouseIn = true;
    }).mouseleave(function() {
        mouseIn = false;
        mouseDown = false;
        clearInterval(interval);
        callback(1);
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why setTimeout and setInterval?
It adds an initial pause before firing "hold down". You could just use the initial timeout on setInterval(), but this allows for a little more flexibility. Are you sensing a code smell?
Yes, I have a good nose for those :-) The timeout might start the interval and set mouseDown=true long after it is released. Click the button once, and then hover it after one second or so.
Ahhh :) Good catch! So would you recommend moving the mouseDown = true; out of the setTimeout and into the parent method, or moving to just the setInterval?
1

Check the following jsFiddle. It does what you're looking for (at least, I hope it does). Let me know if it's not clear and I'll see if I can clear it up a bit.

http://jsfiddle.net/s9nUr/

Here's the code from the fiddle, if you're not interested in seeing it in action. Please note the use of jQuery.

var x= 0;
var interval;

var push = function(val) {

}

var hold = function() {
    if ( x === 0 ) {
        console.log('x is 0');    
    }
    if ( x === 1 ) {
        console.log('x is 1');        
    }
}

$('#myButton').on('mousedown', function() {
    x= 0;
    push(x); 

    interval = setInterval(hold, 500);
});

$('#myButton').on('mouseup', function() {
    x = 1;        
    push(x); 

    clearInterval(interval);
});

Comments

0

maybe creating one function with 2 arguments: value and action. so when button is pushed -> myfunction(x,"pushed") ... and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.