From 298a6e04c2f3cfe68b10b11c601f7c00023ec664 Mon Sep 17 00:00:00 2001 From: shiyu_huang Date: Mon, 15 Aug 2022 16:29:06 +0800 Subject: [PATCH] IssueNo:I5MAOI Description:Fix setInterval bug while interval is zero Sig: SIG_ApplicationFramework Feature or Bugfix:Bugfix Binary Source:No Signed-off-by: shiyu_huang Change-Id: Ib975fb1314c31d44e64a9780e45556b0dbe223fa --- frameworks/native/runtime/js_timer.cpp | 9 +++++---- frameworks/simulator/ability_simulator/src/js_timer.cpp | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/frameworks/native/runtime/js_timer.cpp b/frameworks/native/runtime/js_timer.cpp index bbbc8db55e..721fd39336 100644 --- a/frameworks/native/runtime/js_timer.cpp +++ b/frameworks/native/runtime/js_timer.cpp @@ -67,15 +67,15 @@ private: class JsTimer final { public: JsTimer(JsRuntime& jsRuntime, const std::shared_ptr& jsFunction, const std::string &name, - int64_t interval) - : jsRuntime_(jsRuntime), jsFunction_(jsFunction), name_(name), interval_(interval) + int64_t interval, bool isInterval) + : jsRuntime_(jsRuntime), jsFunction_(jsFunction), name_(name), interval_(interval), isInterval_(isInterval) {} ~JsTimer() = default; void operator()() const { - if (interval_ > 0) { + if (isInterval_) { jsRuntime_.PostTask(*this, name_, interval_); } #ifdef SUPPORT_GRAPHICS @@ -108,6 +108,7 @@ private: std::vector> jsArgs_; std::string name_; int64_t interval_ = 0; + bool isInterval_ = false; #ifdef SUPPORT_GRAPHICS int32_t containerScopeId_ = ContainerScope::CurrentId(); #endif @@ -138,7 +139,7 @@ NativeValue* StartTimeoutOrInterval(NativeEngine* engine, NativeCallbackInfo* in // create timer task JsRuntime& jsRuntime = *reinterpret_cast(engine->GetJsEngine()); - JsTimer task(jsRuntime, jsFunction, name, isInterval ? delayTime : 0); + JsTimer task(jsRuntime, jsFunction, name, delayTime, isInterval); for (size_t index = 2; index < info->argc; ++index) { task.PushArgs(std::shared_ptr(engine->CreateReference(info->argv[index], 1))); } diff --git a/frameworks/simulator/ability_simulator/src/js_timer.cpp b/frameworks/simulator/ability_simulator/src/js_timer.cpp index 63d20a986e..4fa5b113fa 100644 --- a/frameworks/simulator/ability_simulator/src/js_timer.cpp +++ b/frameworks/simulator/ability_simulator/src/js_timer.cpp @@ -125,7 +125,12 @@ NativeValue* StartTimeoutOrInterval(NativeEngine* engine, NativeCallbackInfo* in task->PushArgs(std::shared_ptr(engine->CreateReference(info->argv[index], 1))); } - task->Start(delayTime, isInterval ? delayTime : 0); + // if setInterval is called, interval must not be zero for repeat, so set to 1ms + int64_t interval = 0; + if (isInterval) { + interval = delayTime > 0 ? delayTime : 1; + } + task->Start(delayTime, interval); { std::lock_guard lock(g_mutex);