-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
62 lines (49 loc) · 1.27 KB
/
test.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
'use strict';
const TaskLoop = require('./module');
// create our task loop with strict mode
let taskloop = new TaskLoop(true).runTaskLoop();
function test1()
{
let counter = 0;
// add first task run every 1000 milliseconds
taskloop.addTask('first', 1000, (task_id) =>
{
counter ++;
console.log(task_id + ' : ' + counter);
// pause this task for 3 seconds when arriving 5 runs
if (counter == 5)
{
taskloop.pauseTask(task_id);
setTimeout(() => { taskloop.resumeTask(task_id); }, 3000);
console.log(task_id + ' : waiting 3 seconds');
}
// remove this task after 10 runs
if (counter >= 10)
{
taskloop.removeTask(task_id);
console.log(task_id + ' : done');
}
}).executeOnce(); // make this task execute immediately after it's add
}
function test2()
{
// using an object as the task id, run every 5 seconds.
let obj = { id: 'second', foo: 'bar', counter: 0 };
taskloop.addTask(obj, 5000, (task) =>
{
task.counter ++;
console.log(task.id + ' : ' + task.counter);
// stop the task loop after 4 runs
if (task.counter >= 4)
{
// remove all task at this time
taskloop.stopTaskLoop(true);
console.log(task.id + ' : stop and exit');
}
});
}
test1();
test2();
// show all tasks we just added
console.log(taskloop.queryTask());
//...