MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / setInterval

Function setInterval

packages/core/timer/index.android.ts:60–101  ·  view source on GitHub ↗
(callback: Function, milliseconds = 0, ...args)

Source from the content-addressed store, hash-verified

58}
59
60export function setInterval(callback: Function, milliseconds = 0, ...args): number {
61 milliseconds += 0;
62
63 const id = createHandlerAndGetId();
64 const handler = timeoutHandler;
65 const invoke = () => callback(...args);
66 const zoneBound = zonedCallback(invoke);
67 let nextDueTime = Date.now() + milliseconds;
68
69 const runnable = new java.lang.Runnable({
70 run: () => {
71 const executionStart = Date.now();
72 zoneBound();
73
74 if (timeoutCallbacks[id]) {
75 const executionTime = Date.now() - executionStart;
76
77 // Update the next due time based on when this callback was supposed to execute
78 nextDueTime += milliseconds;
79
80 // If the callback took longer than the interval, skip ahead to avoid catch-up
81 const now = Date.now();
82 if (nextDueTime <= now) {
83 // Calculate how many intervals we should skip
84 const missedIntervals = Math.floor((now - nextDueTime) / milliseconds);
85 nextDueTime += (missedIntervals + 1) * milliseconds;
86 }
87
88 const delay = Math.max(0, nextDueTime - now);
89 handler.postDelayed(runnable, long(delay));
90 }
91 },
92 });
93
94 if (!timeoutCallbacks[id]) {
95 timeoutCallbacks[id] = runnable;
96 timeoutCallbacksCb[id] = callback;
97 }
98
99 handler.postDelayed(runnable, long(milliseconds));
100 return id;
101}
102
103export const clearInterval = clearTimeout;

Callers

nothing calls this directly

Calls 1

createHandlerAndGetIdFunction · 0.85

Tested by

no test coverage detected