I've written a small DHTML test script that uses setTimeout to call some function in a specific interval. Right now all it does is add 1 to the previous number in a textbox on the page, then refreshes that value of the textbox with the new number. It does this every 100ms.
At the same time, I have an event handling function assigned to various events (onmousemove, onmousedown, etc.) on that same page.
Yes, there is a reason I have a seperate function to refresh the form fields as opposed to updating it directly in 'handleMouseMove' (or any other event handler function).Code:<html> <head> <script language="javascript"> mouseXPos = 0; mouseYPos = 0; m1 = ''; keyCode = 0; execThread = null threadDelay = 100; function updateData(formFieldID) { if (document.getElementById(formFieldID).value) document.getElementById(formFieldID).value = parseInt(document.getElementById(formFieldID).value) + 1; else document.getElementById(formFieldID).value = 1; } function runThread(formFieldID) { if (execThread) clearTimeout(execThread); updateData(formFieldID); execThread = setTimeout("runThread('" + formFieldID + "')",threadDelay); } function refreshForm() { document.getElementById('MouseXPos').value = mouseXPos; document.getElementById('MouseYPos').value = mouseYPos; document.getElementById('MouseButton').value = m1; document.getElementById('KeyButton').value = keyCode; } function handleMouseMove(e) { mouseXPos = e.pageX; mouseYPos = e.pageY; } function handleMouseDown(e) { m1 = "#"; } function handleMouseUp(e) { m1 = ""; } function handleKeyDown(e) { keyCode = e.which; } function handleKeyUp(e) { keyCode = ''; } function handle(e) { if (e.type == 'mousemove') handleMouseMove(e); else if (e.type == 'mousedown') handleMouseDown(e); else if (e.type == 'mouseup') handleMouseUp(e); else if (e.type == 'keydown') handleKeyDown(e); else if (e.type == 'keyup') handleKeyUp(e); refreshForm(); } function main() { runThread('threadValue'); } document.onmousemove = handle; document.onmousedown = handle; document.onmouseup = handle; document.onkeydown = handle; document.onkeyup = handle; </script> </head> <body onLoad="main()"> X Pos: <input type="text" id="MouseXPos" size="5"/><br/> Y Pos: <input type="text" id="MouseYPos" size="5"/><br/> Button: <input type="text" id="MouseButton" size="3"/><br/> Key: <input type="text" id="KeyButton" size="3"/><br/><br/> Thread: <input type="text" id="threadValue" size="5"/> </body> </html>
The problem is that the 'thread' seems to work fine... until you move the mouse. I've even tried this with mouse clicks and constant keypresses on the keyboard and it doesn't seem the have the same problem... only when you move the mouse (and I mean MOVE the mouse... drag it all over the place for 5 seconds kind-of-thing). The thread that updates the value in the textbox field stops completely until the mousemove event is handled.
Anyone have any ideas why this happens and what might be able to be done to remedy it? I appreciate it.
- coredumped.



Reply With Quote


Bookmarks