10 examples of 'javascript timer loop' in JavaScript

Every line of 'javascript timer loop' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
16export function loop(interval: number, callback: (...args: any[]) => any) {
17 const timer = setInterval(callback, interval)
18 return () => clearInterval(timer)
19}
61private startTimer() {
62 this.el.classList.remove(`stopped`)
63 this.startTime = Date.now()
64 this.timerInterval = window.setInterval(() => {
65 const timeLeft = this.timeLeftMilliseconds()
66 if (timeLeft <= 0) {
67 this.stopTimer()
68 this.notifyCompletion()
69 } else {
70 this.displayTimeLeft(formattedTimeSeconds(timeLeft))
71 }
72 }, updateIntervalMs)
73}
36private startTimer() {
37 this.el.classList.remove(`stopped`)
38 this.startTime = Date.now()
39 this.timerInterval = window.setInterval(() => {
40 const timeLeft = this.timeLeftMilliseconds()
41 if (timeLeft <= 0) {
42 this.stopTimer()
43 this.notifyCompletion()
44 } else {
45 this.displayTimeLeft(formattedTimeSeconds(timeLeft))
46 }
47 }, updateInterval)
48}
33_setTimeout(callback, time) {
34 this._timer = setTimeout(() => {
35 callback()
36 this._finish = true
37 this._rest = null
38 }, time / this._rate)
39
40 this._finish = false
41 this._time = time
42 this._startTime = new Date()
43 this._callback = callback
44 this._rest = null
45}
131(function timer() {
132 diff = that.duration - Math.floor((Date.now() - start) / 1000);
133
134 if (diff > 0) {
135 that.timeoutId = setTimeout(timer, that.granularity);
136 that.tick(diff);
137 } else {
138 that.running = false;
139 that.tick(0);
140 that.expire();
141 }
142}());
8setTimeout(function timer() {
9 console.log(j);
10}, i * 1000);
218return function timerLoop(nonblocking) {
219 var now, timer;
220
221 // Note: index 0 unused in timersByTime
222 while (timersByTime.length > 1) {
223 timer = timersByTime[1];
224
225 if (timer.cancel) {
226 delete timersByHandle[timer.handle];
227 heapPop(timersByTime, timerCompare);
228 } else {
229 now = $time * 1000;
230 if (timer.time <= now) {
231 inCallback = true;
232 try {
233 if (typeof timer.code === "function") {
234 timer.code.apply(undefined, timer.args);
235 } else {
236 eval(timer.code);
237 }
238 } finally {
239 inCallback = false;
240 }
241
242 if (timer.repeat > 0 && !timer.cancel) {
243 timer.time += timer.repeat;
244 heapFixDown(timersByTime, 1, timerCompare);
245 } else {
246 delete timersByHandle[timer.handle];
247 heapPop(timersByTime, timerCompare);
248 }
249 } else if (!nonblocking) {
250 sleep(timer.time - now);
251 } else {
252 return true;
253 }
254 }
255 }
256
257 return false;
258};
173updateTimer () {
174
175 if ( this.intervalId ) clearInterval ( this.intervalId );
176
177 if ( !this.itemProps.visibility ) return;
178
179 this.intervalId = setInterval ( this.updateText.bind ( this ), 1000 );
180
181}
296function timer() {
297 firstClick = true;
298 counter = setInterval(function () {
299 seconds += 1;
300
301 second = (seconds % 60);
302 minute = parseInt(seconds / 60);
303
304 // if second < 10 add a 0 before the seconds
305 if (second < 10) {
306 second = `0${(seconds % 60)}`;
307 }
308
309 // if minutes < 10 add a 0 before the minutes
310 if (minute < 10) {
311 minute = `0${parseInt(seconds / 60)}`;
312 }
313
314 $('#time-info').html(`${minute} : ${second}`);
315
316 }, 1000);
317}
14beginTimer () {
15 if (this.intervalId != null) {
16 window.clearInterval(this.intervalId);
17 }
18 var rt = this;
19 this.intervalId = window.setInterval(function () {
20 rt.tickTask();
21 }, 32);
22 Project.saving = false;
23 // Prims.time = (new Date() - 0);
24 this.threadsRunning = [];
25}

Related snippets