views:

228

answers:

3

Hi, I would like to know if someone knows how to make a function repeat over and over while the mouse is press, I don't know how to make it work. I know in prototype you can take events like

$('id').observe("click",function(event){}) 
$('id').observe("leave",function(event){})
$('id').observe("change",function(event){})
//etc...

but is something like $('id').observe("whilemousepress",function(event){}) :P //I know there is not any event in javascript but I would like to emulate.

thanks...

+3  A: 

I can't comment on the prototype specifics, but you could probably do this by creating an interval using setInterval() that is started on mousedown and is stopped using .clearInterval() on mouseup.

gnarf
mmmmm nice... let me try and comment later... thanks for you time...
nahum
+1  A: 

There is an event for when the mouse is down mousedown and one for when the mouse is up mouseup. Just start your events when the mouse is pressed and stop them when the button is released.

$('id').observe("mousedown",function(event){
    // Do whatever you want
})

$('id').observe("mouseup",function(event){
    // Stop the events starts when the mouse when down
})
John Conde
Thanks John I think this is works just fine for me thanks for your time ...
nahum
I guess you need the setInterval for make it works.
nahum
A: 

ok... I guess both are correct what I did is :

        $('right').observe('mousedown',function(event){ intervalRigth = setInterval(this.name + ".dosomething()",50); }.bind(this));
        $('left').observe('mousedown',function(event){ intervalLeft = setInterval(this.name + ".dosomething()",50); }.bind(this));


        $('right').observe('mouseup',function(event){ clearInterval(intervalRigth ); }.bind(this));
        $('left').observe('mouseup',function(event){ clearInterval(intervalLeft ); }.bind(this));            

//so I guess a mix of both answer are right thanks =)

nahum