var module = (function() { return { foo: function(count) { var timer = setInterval(function() { console.log(count); if (count == 20) clearInterval(timer); count++; }, 1000); } } })(); window.onload = function() { module.foo(0); }