FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

[JS] unset 'setTimeout'

 


DanielXP
Hey,

I have a JS function called 'starttimer'

I have this code

Code:
setTimeout("setTimeout()", 1000)


To make it count every second.

I was wondering what code is there to stop the timer?

Instead of using a code like

Code:
setTimeout("setTimeout()", 1000000)


Thanks,
Daniel[/code][/quote]
MrBlueSky
The function clearTimeout cancels a timeout. Use the id which setTimeout returns with the function clearTimeout:

Code:

var id = setTimeout("setTimeout()", 1000);

// do stuff

clearTimeout(id);
DanielXP
Thanks already found out to do it but didn't get a chance to post..

Mum dragged me out on a shopping trip Sad
Stubru Freak
If you want something once, use setTimeout, if you want it every second, use setInterval.
DanielXP
Stubru Freak wrote:
If you want something once, use setTimeout, if you want it every second, use setInterval.


I tryed the setinterval and the timer went from 30 > 19 > 7 >3 > 0
Stubru Freak
DanielXP wrote:
Stubru Freak wrote:
If you want something once, use setTimeout, if you want it every second, use setInterval.


I tryed the setinterval and the timer went from 30 > 19 > 7 >3 > 0


If you use setTimeout, you're probably doing this:

Code:
function someFunc(){
// Do something
setTimeout(someFunc, 1000);
}

someFunc();


So you're always making a new timer. After a while, it will get slower. A timer starting from one hour will probably take half a minute longer to complete.

If you're using setInterval, you just do this:

Code:
function someFunc(){
// Do something
}

setInterval(someFunc, 1000);


So you start the interval only once, and you don't redeclare it in the function.
DanielXP
O yer that is what I done.

Thanks + silly me.
Daniel
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.