-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneTwoThree.js
68 lines (64 loc) · 2.2 KB
/
oneTwoThree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: A simple incremental counter at fixed or random intervals
//:: Author: Mark Hayden
//:: Author Site: http://markhayden.me
//:: License: Free General Public License (GPL)
//:: Version: 1.0.0
//:: Date: 12.23.2013
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function oneTwoThree(settings){
window.addCommas = function(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
// SET THE LOOPING ACTION
this.counter_loop = function(){
var me = this;
var big = settings.refresh_high;
var small = settings.refresh_low;
setTimeout(function() {
me.counter_count();
}, Math.floor((Math.random()*big)+small))
}
// GET & SET HELPER FILE
this.counter_count = function(){
var increment = Math.floor((Math.random()*settings.increment_high)+settings.increment_low);
var dats = {'increment':increment, file:settings.file};
var me = this;
$.ajax({
type: "POST",
url: "bump.php",
data:dats,
success: function(data){
if(settings.format == ","){
$(settings.target).html(settings.prefix+window.addCommas(data));
}else{
$(settings.target).html(settings.prefix+data);
}
}
});
this.counter_loop();
}
this.counter_count();
}
/* SAMPLE INITIALIZATION
var counter = new oneTwoThree({
increment_low:1, // low end of the random number for count increment
increment_high:100, // high end of the random number for count increment
refresh_low:250, // low end of the random number for refresh interval
refresh_high:2000, // high end of the random number for refresh interval
prefix:"", // append a string to the beginning of the returned value
format: ",", // add comma divider for three number places
file:"count.txt", // what file holds the counter
target:"#count"
});
*/