Last active
March 4, 2020 23:39
Revisions
-
WebReflection revised this gist
Dec 12, 2013 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
WebReflection revised this gist
Nov 18, 2013 . 1 changed file with 34 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() { // 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 (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; }(500)); -
WebReflection revised this gist
Nov 18, 2013 . 1 changed file with 27 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,35 +1,44 @@ /*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 ); } 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; } Delayed.delay = delay; return Delayed; }(500)); -
WebReflection revised this gist
Nov 10, 2013 . 1 changed file with 19 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { 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 ); } 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; -
WebReflection created this gist
Nov 3, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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);