Skip to content

Instantly share code, notes, and snippets.

@thinsoldier
Last active September 2, 2017 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinsoldier/9017f5395e490818a3a3e530d077b331 to your computer and use it in GitHub Desktop.
Save thinsoldier/9017f5395e490818a3a3e530d077b331 to your computer and use it in GitHub Desktop.
fluent setinterval and settimeout
'use strict';
$(document).ready( function(event) {
report('dom ready at ' + (new Date).toLocaleString('en-US', timeFormat) );
// √ works //
// var ss = new Schedule( myfunction ).repeat.every(1).seconds();
// √ works //
// var ss = new Schedule( myfunction ).repeat.every( 0.5 ).seconds();
// √ works //
// var ss = new Schedule( myfunction ).repeat.every( 3 ).seconds();
// √ works //
var ss = new Schedule( myfunction ).after(5).seconds().repeat.every( 2 ).seconds();
// √ works //
// var ss = new Schedule( myfunction ).once.after(6).seconds();
// To do:
// .repeat.every(2).minutes();
// .repeat.every(0.5).minutes();
// .repeat.every(1.5).hours();
// .after(5).minutes.repeat.every(2).hours();
// .once.after(4).minutes();
// .once after(2.5).hours();
});
//-----------------------------
var timeFormat = { hour: 'numeric',minute:'numeric', second:'numeric', hour12: true };
function report( message ){
$('body').prepend( $('<p>').text(message) );
}
var myfunction = function(){
var time = (new Date).toLocaleString('en-US', timeFormat);
report('myfunction ran at ' + time);
};
//-----------------------------
var Schedule = function( taskFunction )
{
var ROOT = this;
this.task = taskFunction;
this.interval_id = null;
this.interval_period = null;
this.interval_payload = null;
this.wait_period = null;
this.makeSetInterval = function()
{
this.interval_payload = function(){ this.interval_id = setInterval(this.task, this.interval_period); }
report('created interval payload');
}
this.makeSetTimeout = function()
{
var waitForIt = function(){ ROOT.makeSetInterval(); ROOT.interval_payload(); }
this.timeout_id = setTimeout( waitForIt, this.wait_period );
}
this.once = {
after : function( unmeasured ){
this.unmeasured = unmeasured;
return this;
},
seconds: function(){
report(`waiting ${this.unmeasured} seconds before running the task function ONCE`);
ROOT.wait_period = this.unmeasured * 1000;
ROOT.timeout_id = setTimeout( ROOT.task, ROOT.wait_period );
},
};
this.after = function( unmeasured ){
this.unmeasured = unmeasured; // unitless countdown
this.seconds = function(){
report(`waiting ${this.unmeasured} seconds before running the interval payload`)
this.wait_period = this.unmeasured * 1000;
ROOT.makeSetTimeout();
return this;
}
return this;
}
this.repeat = {
every : function( unmeasured ){
this.unmeasured = unmeasured; //unitless_repeat
return this;
},
seconds: function(){
ROOT.interval_period = this.unmeasured * 1000;
if( ! ROOT.wait_period ){
ROOT.makeSetInterval();
ROOT.interval_payload();
} else { report('waiting...'); }
return this;
},
};
return this;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment