I like to spice my sites up with effects (not too many) but i generally have 3-4 intervals going. Just wondering if anyone knows if i'm stretching barriers or can i have a lot more if i wanted?
I would suggest any setinterval is too many, you should always use settimeout instead. The reason for this is that javascript is single threaded. This means that if some code (maybe the function in setinterval) blocks the timeout call, setinterval will continue to issue more calls to the function. With small intervals this can result in function calls stacking up causing EXTREMELY hard to find/reproduce bugs.
Best practice here is to just use settimeout within the function itself. So if you previously had:
function foobar()
{
// do some stuff
}
setInterval(foobar, 100)
You should change it to:
function foobar()
{
//do some stuff
setTimeOut(foobar, 100)
}
This has the added benefit that you can add a bit of logic in to check if you actually want to run the function again.
If you are afraid of having multiple timers, use 1 timer and make the effects fire every nth time the timer counts down. It depends on what you are trying to do though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With