siri Posted February 14, 2008 Share Posted February 14, 2008 Hi,There is a problem in the onmousedown event of the follwing code. In the mousedown event I am trying to clear theTimer Id but still the setInterval is getting executed so I am not getting a slider effect.Can anyone suggest me a solution for this problem ?Here is the foll code:*******************************************<script type="text/javascript"> var pos=0; var timer; var flag=1; function scr() { if(flag ==1) { pos += 1; document.getElementById("d1").style.left=pos; document.getElementById("t1").innerText=pos; timer = setTimeout("scr()",150) } } function stop() { window.clearTimeout(timer); flag=0; }*********************************************Html Part:-----------------------<div style="border:solid 1px black"> <div id="d1" style="position:relative;width:100px;"> <table style="width: 61px; height: 49px" > <tr><td id="t1" style="background-image:url(slider001.gif); vertical-align:top; text-align:center; color:white; font-weight:bold; cursor:pointer" onmousedown ="scr()" onmouseup="stop()"></td></tr> </table> </div> <div style="position:absolute; left: 12px; top: 93px;"> <img src="slider002.gif" alt="scale" /> </div> </div> Link to comment Share on other sites More sharing options...
Reg Edit Posted February 14, 2008 Share Posted February 14, 2008 [...] I am not getting a slider effect.Funny... I pasted your code and it works for me in both IE7 and FF. What browser are you trying it in? Link to comment Share on other sites More sharing options...
siri Posted February 14, 2008 Author Share Posted February 14, 2008 When I keep the mouse pressed for more than 6 seconds then the sliding doesnot stop instantly after I release the mouse. Link to comment Share on other sites More sharing options...
jesh Posted February 14, 2008 Share Posted February 14, 2008 I tend to write animation code like this: var isAnimating = false;function animate(){ if(isAnimating) { // set the positions... setTimeout(animate, 200); }}function start(){ isAnimating = true; animate();}function stop(){ isAnimating = false;}document.onmousedown = start;document.onmouseup = stop; This way you don't have to worry about keeping track of timeout ids - all you have to worry about is the value of isAnimating. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.