Reputation: 3565
I'm using a function that runs on a group of elements to update each one.
I need to execute a serialization function when the updating is done.
Currently the serialization runs too many times (the number of elements), and I only want it to run once.
I tried declaring a timer variable and disable it each time but the last:
clearTimeout(timer);
timer = setTimeout(function(){console.log("tester")}, 5000);
return this;
but this doesn't seem to work and while the function is timed out, it runs multiple times.
Any ideas on how to fix this?
Thanks!
Upvotes: 0
Views: 4525
Reputation: 956
I also think the problem is elsewhere : setTimeout only executes once by declaration, so if it fires more than once, it can only be because its been declared numerous time.
Can you check if you don't run the parent function more than should be ? To unbug this, set counters that you will console.log() for each of your functions. Go back 3/4 levels in the tree and check if none of them are counted twice or more.
Modifying your event attach method from anything to jQuery on or bind can be a helper also.
EDIT : to strictly answer your question :
var counter = 0; //should be out of your function scope
setTimeout(function(){
counter +=1;
if (counter == 1){
//yourFunction()
}
},5000)
Upvotes: 2