Jump to content

Timer problem


siri

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...