Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active March 4, 2020 23:39

Revisions

  1. WebReflection revised this gist Dec 12, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Delayed.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ var Delayed = (function (delay) {

    // 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
    @@ -34,6 +35,7 @@ var Delayed = (function (delay) {
    function Delayed(callback, delay) {
    // the returned wrapper
    function delayed() {
    /*jshint validthis: true */
    // ensure the right clear method
    clear.call(delayed);
    // re-set the timeout (will be again in waiting state)
  2. WebReflection revised this gist Nov 18, 2013. 1 changed file with 34 additions and 11 deletions.
    45 changes: 34 additions & 11 deletions Delayed.js
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,67 @@
    /*jslint browser: true, indent: 2 */
    var Delayed = (function (delay) {

    /*! Andrea Giammarchi - Mit Style License */
    // https://gist.github.com/WebReflection/7286687

    'use strict';

    // method shared across all delayed wrappers
    function clear() {
    if (this.waiting !== Infinity) {
    // 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) {
    clearTimeout(this.waiting);
    }
    // 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() {
    // ensure the right clear method
    clear.call(delayed);
    // re-set the timeout (still waiting after)
    // re-set the timeout (will be again in waiting state)
    delayed.waiting = setTimeout(
    invoke,
    delay,
    delayed,
    callback,
    this,
    arguments
    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;
    }
    // a ways to stop the execution
    // exposing a method to stop the execution while waiting
    delayed.clear = clear;
    // although waiting will be false only once executed
    // until it's executed first time, the state is waiting
    delayed.waiting = Infinity;
    // **or** if explicitly blocked by a user
    // 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;

    }(500));
  3. WebReflection revised this gist Nov 18, 2013. 1 changed file with 27 additions and 18 deletions.
    45 changes: 27 additions & 18 deletions Delayed.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,44 @@
    /*! (C) Andrea Giammarchi */
    // https://gist.github.com/WebReflection/7286687
    var Delayed = function(delay){
    /*jslint browser: true, indent: 2 */
    var Delayed = (function (delay) {
    /*! Andrea Giammarchi - Mit Style License */
    // https://gist.github.com/WebReflection/7286687
    'use strict';
    function clear() {
    if (this.waiting !== Infinity) {
    clearTimeout(this.waiting);
    }
    this.waiting = 0;
    }
    function invoke(delayed, callback, context, args) {
    // mark as consumed already, not waiting anymore
    delayed.waiting = 0;
    // finally invoke the callback
    callback.apply(context, args);
    }
    function Delayed(callback, delay) {
    function delayed() {
    // ensure the right clear method
    clear.call(delayed);
    // re-set the timeout (still waiting after)
    delayed.waiting = setTimeout(
    invoke, delay, delayed, callback, this, arguments
    invoke,
    delay,
    delayed,
    callback,
    this,
    arguments
    );
    }
    if (!delay) delay = Delayed.delay;
    if (!delay) {
    delay = Delayed.delay;
    }
    // a ways to stop the execution
    delayed.clear = clear;
    // although waiting will be false only once executed
    delayed.waiting = Infinity;
    // **or** if explicitly blocked by a user
    return delayed;
    }
    function clear() {
    if (this.waiting !== Infinity) {
    clearTimeout(this.waiting);
    }
    this.waiting = 0;
    }
    function invoke(delayed, callback, context, args) {
    // mark as consumed already, not waiting anymore
    delayed.waiting = 0;
    // finally invoke the callback
    callback.apply(context, args);
    }
    Delayed.delay = delay;
    return Delayed;
    }(500);
    }(500));
  4. WebReflection revised this gist Nov 10, 2013. 1 changed file with 19 additions and 13 deletions.
    32 changes: 19 additions & 13 deletions Delayed.js
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,33 @@
    /*! (C) Andrea Giammarchi */
    // https://gist.github.com/WebReflection/7286687
    var Delayed = function(delay){
    function Delayed(callback, delay) {
    if (!delay) delay = Delayed.delay;
    function delayed() {
    clear();
    delayed._ = setTimeout(
    invoke, delay, callback, this, arguments, delayed
    // ensure the right clear method
    clear.call(delayed);
    // re-set the timeout (still waiting after)
    delayed.waiting = setTimeout(
    invoke, delay, delayed, callback, this, arguments
    );
    }
    function clear() {
    clearTimeout(delayed._);
    }
    if (!delay) delay = Delayed.delay;
    // a ways to stop the execution
    delayed.clear = clear;
    delayed._ = 0;
    // although waiting will be false only once executed
    delayed.waiting = Infinity;
    // **or** if explicitly blocked by a user
    return delayed;
    }
    function clear() {
    clearTimeout(this._);
    this._ = 0;
    return this;
    if (this.waiting !== Infinity) {
    clearTimeout(this.waiting);
    }
    this.waiting = 0;
    }
    function invoke(callback, context, args, delayed) {
    delayed._ = 0;
    function invoke(delayed, callback, context, args) {
    // mark as consumed already, not waiting anymore
    delayed.waiting = 0;
    // finally invoke the callback
    callback.apply(context, args);
    }
    Delayed.delay = delay;
  5. WebReflection created this gist Nov 3, 2013.
    29 changes: 29 additions & 0 deletions Delayed.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /*! (C) Andrea Giammarchi */
    var Delayed = function(delay){
    function Delayed(callback, delay) {
    if (!delay) delay = Delayed.delay;
    function delayed() {
    clear();
    delayed._ = setTimeout(
    invoke, delay, callback, this, arguments, delayed
    );
    }
    function clear() {
    clearTimeout(delayed._);
    }
    delayed.clear = clear;
    delayed._ = 0;
    return delayed;
    }
    function clear() {
    clearTimeout(this._);
    this._ = 0;
    return this;
    }
    function invoke(callback, context, args, delayed) {
    delayed._ = 0;
    callback.apply(context, args);
    }
    Delayed.delay = delay;
    return Delayed;
    }(500);