-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.js
116 lines (87 loc) · 2.82 KB
/
async.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// setTimeout(() => {
// console.log('!');
// }, 3000);
// setTimeout(() => {
// console.log('World');
// }, 1000);
// console.log('Hello');
// 1. Write a JavaScript function named delayLog that loops through the numbers from 1 to 10,
// and logs each number after that number of seconds. It should log 1 after 1 second, 2 after 2 seconds, etc.
// Note that the computation of the time is not dependent on when a previous number was logged.
// This means that for 10 numbers a total of 10 seconds would have passed.
// function makeLogger(number) {
// return function() {
// console.log(number);
// }
// }
// function delayLog() {
// let counter = 1;
// do {
// let logger = makeLogger(counter);
// setTimeout(logger, counter * 1000);
// counter += 1;
// } while (counter <= 10);
// }
// delayLog();
// 2. In what sequence will the JavaScript runtime run the following lines of code? Number them from 1-8 to show the order of execution.
// setTimeout(() => { // 1
// console.log('Once'); // 5
// }, 1000);
// setTimeout(() => { // 2
// console.log('upon');
// }, 3000); // 7
// setTimeout(() => { // 3
// console.log('a');
// }, 2000); // 6
// setTimeout(() => { // 4
// console.log('time');
// }, 4000); // 8
// 3. In what sequence does the JavaScript runtime run the functions q, d, n, z, s, f, and g in the following code?
// setTimeout(() => {
// setTimeout(() => {
// q(); // 35 mil = 7
// }, 15);
// d(); // 10 mil = 3
// setTimeout(() => {
// n();
// }, 5); // 15 mil = 5
// z(); // 10 mil = 4
// }, 10);
// setTimeout(() => {
// s(); // 20 mil = 6
// }, 20);
// setTimeout(() => {
// f(); // 0 mill = 2
// });
// g(); // 0 mill = 1
// 4. Write a function named afterNSeconds that takes two arguments: a callback and a time duration in seconds.
// The function should wait for the indicated period, then invoke the callback function.
// function afterNSeconds(callback, time) {
// setTimeout(callback, time * 1000);
// }
// 1. Write a function named startCounting that logs a number to the console every second, starting with 1.
// Each number should be one greater than the previous number.
// let makeUpdateCount = function(number) {
// return function() {
// console.log(number);
// number += 1;
// }
// }
// function startCounting() {
// let count = 1;
// let updateCount = makeUpdateCount(count);
// setInterval(updateCount, 1000);
// }
// startCounting();
// // LS answer
let counterId;
function startCounting() {
let count = 0;
counterId = setInterval(() => {
count += 1;
console.log(count);
}, 1000);
}
function stopCounting() {
clearInterval(counterId)
}