Continuous firing of projectiles using mousedown eventlistener in Three JS [duplicate]

2 weeks ago 6
ARTICLE AD BOX

On the mousedown() event, this code starts a repeating timer (every 500ms in this example) that is cancelled as soon as the mouseup() event occurs. This should be adaptable to what you want:

var intervalId; $("#button").mousedown(function() { intervalId = setInterval(do_something, 500); }).mouseup(function() { clearInterval(intervalId); }); function do_something() { // whatever }

See setInterval() for more information on clearing timers.

answered Dec 20, 2009 at 5:56

cletus's user avatar

Sign up to request clarification or add additional context in comments.

4 Comments

If you want the event to occur right away, AND every 500 milliseconds, just call do_something() immediately before calling setInterval.

2012-06-19T09:23:50.343Z+00:00

@TylerCollier Thanks for pointing out. Then it works like the click event plus the repeated action. Very handy. In my case I also had to pass parameters, for this you have to do assign an anonymous function: setInterval( function() { myfunction(valueA, valueB); }, 500 );

2014-08-10T20:16:13.457Z+00:00

2015-05-22T04:47:05.05Z+00:00

@Arbaaz, it's the alert() call that causes this, because the normal flow (with the alert popping up) is disrupted. Change the alert to e.g. console.log('a') and it will stop when you mouseup.

2015-05-22T04:59:40.35Z+00:00

I would use the javascript function setInterval() within a function that gets called on mouse down.

<input type="button" id="button" onmousedown="inter=setInterval(startAction, 1*1000);" onmouseup="clearInterval(inter);" value="click here" /> <script type="text/javascript"> function startAction(){ //whatever you want done } </script>

answered Dec 20, 2009 at 6:00

munch's user avatar

var intervalId; $("#button").mousedown(function() { intervalId = setInterval(do_something, 500); }).mouseup(function() { clearInterval(intervalId); }).mouseleave(function() { //this should help solve the problem that occurs when the mouse leaves the button while pressed down clearInterval(intervalId); }); function do_something() { // whatever }

NotABlueWhale's user avatar

answered Jul 3, 2017 at 13:01

LordKayBanks's user avatar

I see a problem with both solutions listed above.

onmouseup is only triggered if the mouse is released while it is over the button. If the user keeps the mouse down then moves the mouse away before releasing it then clearInterval is never fired and so do_something will fire forever.

You would need to add another event, "onmouseout", that also calls clearInterval.

Dinesh Kanivu's user avatar

Dinesh Kanivu

2,5953 gold badges26 silver badges55 bronze badges

answered Oct 29, 2013 at 9:45

user2931502's user avatar

1 Comment

True but please make this a full answer by adding sample code.

2013-10-29T09:50:52.57Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article