Loop while mousedown?????

P

pleary

I am quite familiar with JavaScript and many other programming
languages as I am a programmer for a living. However, I am having a
heck of a time figuring out this problem. I would like to have a
function loop as long as the mouse button is pressed. I am aware of the
onMouseDown event to fire the loop, but I cann't figure out how to stop
the loop with the onMouseUp event.

I have tried setting a boolean value with the onMouseDown event, but
once I release the mouse button, the boolean value cannot be changed
until the loop is done. So, as the loop is waiting for the value to be
changed, it becomes an infinite loop. Many people have offered
suggestions, but none of them seem to work.

Is there such a method as whileMouseDown, or is it possible to cancel a
function with onMouseUp?

This must be possible.... I am basically trying to scroll through an
image hidden behind a division, much like scrolling through a web page.
When you hold down the arrow key, it scrolls until you let go...

I would greatly appreciate any help anyone can offer. I am using IE 6.0
and I will include a code sample of what I have been doing:

var ismousedown=0;

document.onmousedown = mouseDown;
document.onmouseup = mouseUp;

function mouseDown()
{
ismousedown = 1;
}
function mouseUp()
{
ismousedown = 0;
}
function getMouseValue()
{
return ismousedown;
}
function checkMouse()
{
if(getMouseValue == 0)
{

}else
{
moveDown();
setTimeout("checkMouse()",500);
}
}
 
L

Lee

(e-mail address removed) said:
I am quite familiar with JavaScript and many other programming
languages as I am a programmer for a living. However, I am having a
heck of a time figuring out this problem. I would like to have a
function loop as long as the mouse button is pressed. I am aware of the
onMouseDown event to fire the loop, but I cann't figure out how to stop
the loop with the onMouseUp event.

Use setInterval() to start the loop onMouseDown and clearInterval() to stop it
onMouseUp.
 
S

Stephen Chalmers

if(getMouseValue == 0)

I'm not saying this will fix things, but if the above statement is an
accurate transcript of your working code, I'm sure you'll see the problem.
 
R

RobG

I am quite familiar with JavaScript and many other programming
languages as I am a programmer for a living. However, I am having a
heck of a time figuring out this problem. I would like to have a
function loop as long as the mouse button is pressed. I am aware of the
onMouseDown event to fire the loop, but I cann't figure out how to stop
the loop with the onMouseUp event.

Have a read here, particularly the bit about event handlers.

<URL:http://www.quirksmode.org/js/introevents.html>

I think you want to fire the scroll onmousedown, then cancel it with
onmouseup and maybe onmouseout also.

Hope it helps :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top