How do I reuse this JavaScript timeout closure?
I found the following piece of JavaScript code (maybe here at Stack Overflow?) for implementing a timeout: var delay = (function() { var timer = 0; return function(callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); I'm new to JavaScript, so I'm still trying to wrap my head around closures. If I call delay(firstCallback, 200) in one place and then delay(secondCallback, 200) right after, the first timeout callback is cleared, while the second callback executes successfully. How do I reuse delay in different instances without overwriting other instances? (Not sure if