From 20ca4b013deab55208225ccd44b6a7aa76e70a88 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 25 Oct 2019 11:42:00 +0800 Subject: [PATCH] feat:fix in nodejs use origin setTimeout --- js-framework/src/runtime/sandbox.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index 4e885ada..4e2c74d9 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -255,8 +255,18 @@ let __timerId__ = 0 const timerInfos: Map void, context?: Context }> = new Map +const _setTimeout = global.setTimeout -global.setTimeout = (handler: Function, timeout?: number | undefined, ...args: any[]) => { +const _setInterval = global.setInterval + +const _clearTimeout = global.clearTimeout + +const _clearInterval = global.clearInterval + +global.setTimeout = function (handler: Function, timeout?: number | undefined, ...args: any[]) { + if (global.nativeSetTimer === undefined) { + return Reflect.apply(_setTimeout, undefined, arguments) + } const id = __timerId__++ timerInfos.set(id, { callback: () => { @@ -268,7 +278,10 @@ global.setTimeout = (handler: Function, timeout?: number | undefined, ...args: a nativeSetTimer(id, timeout || 0, false) return id } -global.setInterval = (handler: Function, timeout?: number | undefined, ...args: any[]) => { +global.setInterval = function (handler: Function, timeout?: number | undefined, ...args: any[]) { + if (global.nativeSetTimer === undefined) { + return Reflect.apply(_setInterval, undefined, arguments) + } const id = __timerId__++ timerInfos.set(id, { callback: () => { @@ -280,12 +293,18 @@ global.setInterval = (handler: Function, timeout?: number | undefined, ...args: return id } -global.clearTimeout = (timerId: number) => { +global.clearTimeout = function (timerId: number) { + if (global.nativeClearTimer === undefined) { + return Reflect.apply(_clearTimeout, undefined, arguments) + } timerInfos.delete(timerId) nativeClearTimer(timerId) } -global.clearInterval = (timerId: number) => { +global.clearInterval = function (timerId: number) { + if (global.nativeClearTimer === undefined) { + return Reflect.apply(_clearInterval, undefined, arguments) + } timerInfos.delete(timerId) nativeClearTimer(timerId) }