In my application,I use many setTimeout function,so I am afraid if it will cause peformance problem:
setTimeout(function(){
// do something
},0);
And I found people use this manner:
var t=setTimeout(function(){
// do something
clearTimeout(t);
});
I wonder if it is necessary?
No, object will be destroyed automatically (at least should be). You need to call
clearTimeoutwhen you need to remove already set timeout.Ex: you have set timeout to 5 seconds on hovering some element but user moves out cursor from element before timeout elapsed – so you need to remove already initialized timeout.