From 73bf82e3b58b4f1b41d048aa17cbf14a75127fee Mon Sep 17 00:00:00 2001 From: 3136352472 <3136352472> Date: Thu, 4 Jun 2020 11:45:13 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=B6=85=E6=97=B6bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsruntime/runtime.go | 12 +++++++++--- jsruntime/timeout.go | 5 +++-- static/index.html | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/jsruntime/runtime.go b/jsruntime/runtime.go index de9e22f..c6f0529 100644 --- a/jsruntime/runtime.go +++ b/jsruntime/runtime.go @@ -26,6 +26,7 @@ type JsRuntime struct { mutex sync.Mutex RenderCallBackFun func(string) IsDebugModel bool + isRenderIng bool } type Rely struct { @@ -217,6 +218,7 @@ func (jr *JsRuntime) RunCode(code string) error { } func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{}, cb func(data string)) (err error) { + jr.RenderCallBackFun = cb jr.mutex.Lock() defer jr.mutex.Unlock() @@ -224,9 +226,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{} runtime := jr.runtime mainSrc := jr.MainSrc isLock := true + jr.isRenderIng = true //timeoutSec := jr.TimeoutSec defer func() { + jr.isRenderIng = false isLock = false jr.runtime.ClearInterrupt() }() @@ -266,9 +270,11 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, GoExtData interface{} jr.RenderCallBackFun(data) }) - t := time.AfterFunc(time.Duration(jr.TimeoutSec), func() { - jr.runtime.Interrupt("time out") - jr.runtime.ClearInterrupt() + t := time.AfterFunc(time.Duration(jr.TimeoutSec)*time.Second, func() { + if jr.isRenderIng { + jr.runtime.Interrupt("time out") + jr.runtime.ClearInterrupt() + } }) defer t.Stop() diff --git a/jsruntime/timeout.go b/jsruntime/timeout.go index 60423a8..6bb0241 100644 --- a/jsruntime/timeout.go +++ b/jsruntime/timeout.go @@ -3,6 +3,7 @@ package jsruntime import ( "github.com/dop251/goja" "log" + "time" ) func (jr *JsRuntime) EnableTimeoutFunc() { @@ -10,13 +11,13 @@ func (jr *JsRuntime) EnableTimeoutFunc() { jr.runtime.Set("setTimeout", func(call goja.FunctionCall) goja.Value { if fn, ok := goja.AssertFunction(call.Argument(0)); ok { - //delay := call.Argument(1).ToInteger() + delay := call.Argument(1).ToInteger() var args []goja.Value if len(call.Arguments) > 2 { args = call.Arguments[2:] } // - //time.Sleep(time.Duration(delay) * time.Millisecond) + time.Sleep(time.Duration(delay) * time.Millisecond) fn(nil, args...) } diff --git a/static/index.html b/static/index.html index 4df7112..02e5f58 100644 --- a/static/index.html +++ b/static/index.html @@ -13,7 +13,7 @@