Random Number Generator with speed control
by Jogesh Sharma
HTML
<h1 id="result"></h1>
CSS
/*
* http://webomnizz.com/blog/javascript/settimeout-random-interval-javascript/
*/
JavaScript
/*
* @author Jogesh Sharma
* @param {int} range (optional)
* @returns {numGenrator}
*
*/
var numGenrator = function(element){
this.element = document.getElementById(element);
};
numGenrator.prototype = {
init: function(options){
var that = this;
that.tempRange = options.startRange == undefined ? 1000 : options.startRange;
that.range = that.readCookie('_jnum') == null ? that.tempRange : that.readCookie('_jnum');
that.format = options.currencyFormat == undefined ? false : options.currencyFormat;
// Default Value
that.element.innerHTML = that.format == true ? this.currencyFormat( that.range ) : that.range;
(function loop() {
var rand = Math.round(Math.random() * (2000 - 200)) + 200;
setTimeout(function() {
that.actionInit();
loop();
}, rand);
}());
},
actionInit: function(){
this.range++;
// Use Cookie to Remmber
// Last Value
this.createCookie('_jnum', this.range, 20);
this.element.innerHTML = this.format == true ? this.currencyFormat( this.range ) : this.range;
},
createCookie: function(name, value, days){
if( days ){
var date = new Date;
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expire = "; expires=" + date.toGMTString();
} else {
var expire = "";
}
document.cookie = name + "=" + value + expire + "; path=/";
},
readCookie: function(name){
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return...