How does this "delay" function works
<p>I am using this code to wrap parts of code in it is used like this,</p>

<pre><code>var delay = (function() {
    // SET TIMER
    var timer = 0;
    // RETURN SET TIMEOUT FUNCTION
    return function(callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();​
</code></pre>

<p>I call it like this,</p>

<pre><code>delay(function() {
     .......
}, 1000);
</code></pre>

<p>And it will delay it by 1000 milliseconds, but I do not understand what is going on thanks :)</p>
