-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_intervals_cl.js
53 lines (43 loc) · 1.76 KB
/
js_intervals_cl.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
//Use self-executing anonymous function to create intervalGenerator that will contain variable scope
//and prevent pollution of the global namespace. Also helps to create a library bound to a 'namespace'
//that will not conflict with other libraries.
var intervalGenerator = (function () {
var startTime,
maxTimeInt,
displayOrder;
function initModule() {
startTime = new Date().getTime()
maxTimeInt = 0;
displayOrder = 1;
}
function logIntervalData(order, timeInterval) {
var randNumber = Math.round(Math.random() * 101);
console.log(randNumber + "\t" + order + "\t" + timeInterval + "\t" + displayOrder++);
}
function logCompletionTime(startTime) {
var timeTaken = (new Date().getTime()) - startTime;
timeTaken = Math.round(timeTaken / 1000 * 100) / 100; //convert from MS to seconds and round to 2 digits
console.log("Time to run: " + timeTaken + " s");
}
function run() {
//output column headers
console.log("Random" + "\t" + "Generated Order" + "\t" + "Time" + "\t" + "Display Order");
for (var timeIntCount = 0; timeIntCount < 100; timeIntCount++) {
//Generate random number between 0-5, add 5 so it will 5-10
var timeInterval = (Math.random() * 5) + 5;
setTimeout(logIntervalData, timeInterval * 1000, timeIntCount + 1, timeInterval);
if (timeInterval > maxTimeInt)
maxTimeInt = timeInterval;
}
setTimeout(logCompletionTime, maxTimeInt * 1000, startTime);
}
//Expose public API for the module/library
return {
initModule:initModule,
run:run
}
})();
//Run it
intervalGenerator.initModule();
intervalGenerator.run();