Last active
December 14, 2015 07:09
-
-
Save particle4dev/5048276 to your computer and use it in GitHub Desktop.
This file contains 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 characters
var impose = function(object, func, args){ | |
return func.apply(object, args); | |
} | |
var LoopSomething = (function(){ | |
/** | |
* Define class | |
* | |
* @author Steve Hoang <particles4dev> | |
* @version | |
* @since 0.1 | |
* | |
* @param d delay time | |
* @param t a function to execute | |
* @param [o] a object (has a function to execute) | |
**/ | |
var f = function(d, t, o){ | |
/** | |
* Private property | |
**/ | |
var self = this; | |
var delay = (d === null) ? 1000 : d; | |
var object = (o === null) ? null : o; | |
var todo = t, | |
start = 0,end = 2*delay, | |
run = true; | |
/** | |
* Public property | |
**/ | |
this.timeoutID = null; | |
/** | |
* Get current time | |
* | |
* @return Number | |
* @see void | |
**/ | |
var getTime = function(){ | |
return new Date().getTime(); | |
}; | |
/** | |
* Set a timer to delay | |
* | |
* @param value | |
* @return void | |
* @see void | |
**/ | |
this.setDelay = function(value){ | |
delay = value; | |
start = 0; | |
end = 2*delay; | |
}; | |
/** | |
* Specify a function to execute when the delay time is done | |
* | |
* @param func | |
* @return void | |
* @see void | |
**/ | |
this.setTodo = function(func){ | |
todo = func; | |
}; | |
/** | |
* | |
* | |
* @param f | |
* @param args | |
* @return void | |
* @see void | |
**/ | |
var remind = function(func, args){ | |
if(run === true){ | |
console.log("Object Loop : "); | |
console.log(this); | |
start = getTime(); | |
if(object == null) | |
func.apply(func, args); | |
else | |
func.apply(object, args); | |
var callback = args[args.length-1]; | |
if(typeof callback == 'function') | |
callback(); | |
end = getTime(); | |
self.setup.apply(self, args); | |
} | |
}; | |
/** | |
* | |
* | |
* @param callback | |
* @return void | |
* @see void | |
**/ | |
this.setup = function() { | |
if(run === true){ | |
var args = []; | |
for (var i = 0; i < arguments.length; i++) { | |
args.push(arguments[i]); | |
} | |
var mt = delay - (end - start); | |
if(mt > 0){ | |
this.timeoutID = window.setTimeout(impose, mt, this, remind,[todo, args]); | |
} | |
else{ | |
impose(this, remind, [todo, args]); | |
} | |
} | |
}; | |
/** | |
* exits the loop | |
* | |
* @return void | |
* @see void | |
**/ | |
this.cancel = function() { | |
if(typeof this.timeoutID == "number") { | |
window.clearTimeout(this.timeoutID); | |
delete this.timeoutID; | |
} | |
run = false; | |
console.log('stop loop'); | |
return this; | |
}; | |
/** | |
* Get current state | |
* | |
* @return void | |
* @see void | |
**/ | |
this.getState = function(){ | |
console.log('Run is ' + run); | |
return run; | |
} | |
}; | |
return f; | |
})(); |
This file contains 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 characters
/** | |
* ##### Examples [http://jsbin.com/oseqaw/1/edit] | |
console.log( "Loop Function" ); | |
var a = new LoopSomething(2000, function(m, f){ | |
console.log( m + " " + f ); | |
}); | |
a.setup("hello", "world", function(){ | |
console.log( 'do callback' ); | |
}); | |
var id = setTimeout(function(){ | |
a.cancel(); | |
},9000); | |
**/ | |
console.log( "Loop Object" ); | |
var obj = { | |
firstName:'Steve', | |
lastName :'Hoang', | |
information: function(){ | |
console.log("My name is " + this.firstName + " " + this.lastName); | |
} | |
}; | |
var a = new LoopSomething(2000, obj.information, obj); | |
a.setup(function(){ | |
console.log( 'do callback' ); | |
}); | |
var id = setTimeout(function(){ | |
a.cancel(); | |
},9000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment