You can use the Collate object on your web page.
function Collate(timeout) { this.timeout = timeout || 1000; } Collate.prototype = { time: 0, idle: function() { var t = new Date().getTime(); return (t - this.time > this.timeout && (this.time = t)); }, prefer: function(func) { this.func = func; clearTimeout(this.timer); this.timer = setTimeout(func, this.timeout); } };
If you want the function to run once and not run again within the next 1 second. For example, if you want the user to not submit the form many times, you do this:
var timer = new Collate(3000); //3 seconds button1.onclick = function() { if(timer.idle()) { button1.form.submit(); } else alert("Don't click too quickly!"); } //or on the form tag <script>var submitTimer = new Collate(3000);</script> <form action="post" onsubmit="return submitTimer.idle();">
If you expect the event to fire several times and only want to respond the last time it fires. For example, if you want to search after the user has finished typing, you will do the following:
var timer = new Collate(700);
Kernel james
source share