答:因為即使在Repl.it選擇建立一個JavaScript的repl,Repl.it給你的依舊是Node.js環境的repl……😂
目錄
setTimeout()在Node.js環境會回傳什麼?
Timeout object(<Timeout>)是什麼?
如何看到MDN文件提到的positive integer value
positive integer value是什麼?
那麼setTimeout()在Node.js環境會回傳什麼?
答:回傳 Timeout object
Ref. 1: setTimeout()
returns a Timeout object
that can be used to reference the timeout that was set.
Ref. 2: Returns: <Timeout>
for use with clearTimeout()
Timeout object(<Timeout>)又是什麼?
答:由setTimeout()
或setInterval()
回傳的物件,可以傳給clearTimeout()
或clearInterval()
來取消其他原本預計要執行的行為。
Ref.: This object is created internally and is returned from setTimeout()
and setInterval()
. It can be passed to either clearTimeout()
or clearInterval()
in order to cancel the scheduled actions.
MDN文件說明setTimeout()會回傳positive integer value,如何看到這個結果?
答:在Chrome瀏覽器(按下F12呼叫)的DevTools,貼上程式碼後按下鍵盤Enter執行就可以了。
根據Chrome DevTools的文件,setTimeout()
在Chrome的行為可參考MDN。
positive integer value又是什麼?
答:是一種ID,此ID可以傳給clearTimeout()
來取消timeout行為。
上面的程式碼貼到DevTools後,第二行的console.log("hello world")
會被取消。結果如下圖:
Ref.: The returned timeoutID
is a positive integer value which identifies the timer created by the call to setTimeout()
; this value can be passed to clearTimeout()
to cancel the timeout.