Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
whenever you need to execute something after an interaction that might happen many times (panning, scrolling, etc) … this simple mechanism makes simple to execute that maybe expensive operations only once when things are "quiet" and never before or in the middle, if an action start as "start panning" would be is fired and it's calling the clear(…
/*jslint browser: true, indent: 2 */
var Delayed = (function (delay) {
/*! Andrea Giammarchi - Mit Style License */
'use strict';
// method shared across all delayed wrappers
function clear() {
/*jshint validthis: true */
// Infinity is the "never executed" state
// if fn.clear() is invoke before the execution
// then there is nothing to clear
// same is for already executed state
if (this.waiting !== Infinity || this.waiting !== 0) {
// if it was waiting, better mark as non waiting anymore
// somebody cleared this delayed state so it's reflected
this.waiting = 0;
// method recycled per each setTimeout call
// no need to create extra closures
function invoke(delayed, callback, context, args) {
// mark as consumed already, not waiting anymore
delayed.waiting = 0;
// finally invoke the callback
callback.apply(context, args);
// the delayed wrapper exported function
// accepts a callback and optional delay in millisecons
function Delayed(callback, delay) {
// the returned wrapper
function delayed() {
/*jshint validthis: true */
// ensure the right clear method;
// re-set the timeout (will be again in waiting state)
delayed.waiting = setTimeout(
invoke, // the recycled function
delay, // the specific delay to wait for
delayed, // the wrapper to clean up
callback, // the original callback
this, // the current context
arguments // current list of arguments
// if not specified or 0, the default is used instead
if (!delay) {
delay = Delayed.delay;
// exposing a method to stop the execution while waiting
delayed.clear = clear;
// until it's executed first time, the state is waiting
delayed.waiting = Infinity;
// state will be falsy once cleared or executed
return delayed;
// the default delay per each wrapper, if not specified
Delayed.delay = delay;
// the exported utility
return Delayed;

This comment has been minimized.

Copy link
Owner Author

@WebReflection WebReflection commented Dec 4, 2013

few examples on how to in

If you are targeting IE < 9 browser you might want to use this snippet to normalize setTimeout extra arguments:

 window.setTimeout =f(window.setTimeout);
 window.setInterval =f(window.setInterval);
})(function(f){return function(c,t){var a=[],2);return f(function(){c.apply(this,a)},t)}});

This comment has been minimized.

Copy link

@kentaromiura kentaromiura commented Dec 9, 2013

Apparently jshint get scared by a function referencing this outside an object,
solved by adding /*jshint validthis: true */
after clear() and delayed() method definition,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment