mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Specifications define timers capable of accepting optional parameters. https://www.w3.org/TR/2011/WD-html5-20110525/timers.html#timers This PR goal is to standardize such behavior in NativeScript too.
This commit is contained in:

committed by
Alexander Vakrilov

parent
9866350559
commit
7e39bfb9d4
@ -39,6 +39,23 @@ export function test_setTimeout() {
|
|||||||
TKUnit.assert(completed, "Callback should be called!");
|
TKUnit.assert(completed, "Callback should be called!");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function test_setTimeout_extraArgs() {
|
||||||
|
let completed: boolean;
|
||||||
|
let rnd: number = Math.random();
|
||||||
|
|
||||||
|
// >> timer-set-zero
|
||||||
|
const id = timer.setTimeout((arg) => {
|
||||||
|
// >> (hide)
|
||||||
|
completed = rnd === arg;
|
||||||
|
// << (hide)
|
||||||
|
}, 0, rnd);
|
||||||
|
// << timer-set-zero
|
||||||
|
|
||||||
|
TKUnit.waitUntilReady(() => completed, 0.5, false);
|
||||||
|
timer.clearTimeout(id);
|
||||||
|
TKUnit.assert(completed, "Callback called with expected argument!");
|
||||||
|
};
|
||||||
|
|
||||||
export function test_setTimeout_callbackCalledAfterSpecifiedTime() {
|
export function test_setTimeout_callbackCalledAfterSpecifiedTime() {
|
||||||
let completed = false;
|
let completed = false;
|
||||||
|
|
||||||
@ -110,6 +127,24 @@ export function test_setInterval_callbackCalledDuringPeriod() {
|
|||||||
TKUnit.assert(counter >= expected, "Callback should be raised at least" + expected + "times! Callback raised " + counter + " times.");
|
TKUnit.assert(counter >= expected, "Callback should be raised at least" + expected + "times! Callback raised " + counter + " times.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function test_setInterval_callbackCalledWithExtraArgs() {
|
||||||
|
let counter: number = 0;
|
||||||
|
let expected: number = 4;
|
||||||
|
let rnd: number = Math.random();
|
||||||
|
|
||||||
|
// >> timer-set-expression
|
||||||
|
const id = timer.setInterval((arg) => {
|
||||||
|
// >> (hide)
|
||||||
|
counter += arg === rnd ? 1 : -1;
|
||||||
|
// << (hide)
|
||||||
|
}, 50, rnd);
|
||||||
|
// << timer-set-expression
|
||||||
|
|
||||||
|
TKUnit.waitUntilReady(() => counter >= expected, 0.25, false);
|
||||||
|
timer.clearInterval(id);
|
||||||
|
TKUnit.assert(counter >= expected, "Callback raised " + counter + " times with arguments.");
|
||||||
|
};
|
||||||
|
|
||||||
export function test_setInterval_callbackShouldBeCleared() {
|
export function test_setInterval_callbackShouldBeCleared() {
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
|
|
||||||
|
@ -14,9 +14,10 @@ function createHandlerAndGetId(): number {
|
|||||||
return timerId;
|
return timerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setTimeout(callback: Function, milliseconds = 0): number {
|
export function setTimeout(callback: Function, milliseconds = 0, ...args): number {
|
||||||
const id = createHandlerAndGetId();
|
const id = createHandlerAndGetId();
|
||||||
const zoneBound = zonedCallback(callback);
|
const invoke = () => callback(...args);
|
||||||
|
const zoneBound = zonedCallback(invoke);
|
||||||
|
|
||||||
var runnable = new java.lang.Runnable({
|
var runnable = new java.lang.Runnable({
|
||||||
run: () => {
|
run: () => {
|
||||||
@ -45,10 +46,11 @@ export function clearTimeout(id: number): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setInterval(callback: Function, milliseconds = 0): number {
|
export function setInterval(callback: Function, milliseconds = 0, ...args): number {
|
||||||
const id = createHandlerAndGetId();
|
const id = createHandlerAndGetId();
|
||||||
const handler = timeoutHandler;
|
const handler = timeoutHandler;
|
||||||
const zoneBound = zonedCallback(callback);
|
const invoke = () => callback(...args);
|
||||||
|
const zoneBound = zonedCallback(invoke);
|
||||||
|
|
||||||
var runnable = new java.lang.Runnable({
|
var runnable = new java.lang.Runnable({
|
||||||
run: () => {
|
run: () => {
|
||||||
|
6
tns-core-modules/timer/timer.d.ts
vendored
6
tns-core-modules/timer/timer.d.ts
vendored
@ -7,8 +7,9 @@
|
|||||||
* Calls a function after a specified delay.
|
* Calls a function after a specified delay.
|
||||||
* @param callback The function to be called.
|
* @param callback The function to be called.
|
||||||
* @param milliseconds The time to wait before the function is called. Defaults to 0.
|
* @param milliseconds The time to wait before the function is called. Defaults to 0.
|
||||||
|
* @param args One or more parameter to use once the function is called. Defaults to no parameters.
|
||||||
*/
|
*/
|
||||||
export function setTimeout(callback: Function, milliseconds?: number): number;
|
export function setTimeout(callback: Function, milliseconds?: number, ...args: any[]): number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the delay set by a call to the setTimeout function.
|
* Clears the delay set by a call to the setTimeout function.
|
||||||
@ -20,8 +21,9 @@ export function clearTimeout(id: number): void;
|
|||||||
* Calls a function repeatedly with a delay between each call.
|
* Calls a function repeatedly with a delay between each call.
|
||||||
* @param callback The function to be called.
|
* @param callback The function to be called.
|
||||||
* @param milliseconds The delay between each function call.
|
* @param milliseconds The delay between each function call.
|
||||||
|
* @param args One or more parameter to use once the function is called. Defaults to no parameters.
|
||||||
*/
|
*/
|
||||||
export function setInterval(callback: Function, milliseconds?: number): number;
|
export function setInterval(callback: Function, milliseconds?: number, ...args: any[]): number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears repeated function which was set up by calling setInterval().
|
* Clears repeated function which was set up by calling setInterval().
|
||||||
|
@ -63,8 +63,9 @@ function createTimerAndGetId(callback: Function, milliseconds: number, shouldRep
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setTimeout(callback: Function, milliseconds = 0): number {
|
export function setTimeout(callback: Function, milliseconds = 0, ...args): number {
|
||||||
return createTimerAndGetId(zonedCallback(callback), milliseconds, false);
|
let invoke = () => callback(...args);
|
||||||
|
return createTimerAndGetId(zonedCallback(invoke), milliseconds, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearTimeout(id: number): void {
|
export function clearTimeout(id: number): void {
|
||||||
@ -74,8 +75,9 @@ export function clearTimeout(id: number): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setInterval(callback: Function, milliseconds = 0): number {
|
export function setInterval(callback: Function, milliseconds = 0, ...args): number {
|
||||||
return createTimerAndGetId(zonedCallback(callback), milliseconds, true);
|
let invoke = () => callback(...args);
|
||||||
|
return createTimerAndGetId(zonedCallback(invoke), milliseconds, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
export var clearInterval = clearTimeout;
|
export var clearInterval = clearTimeout;
|
||||||
|
Reference in New Issue
Block a user