最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout and V8 - Stack Overflow

programmeradmin12浏览0评论

I've installed V8 standalone and execute javascript code like this: ./d8 source.js. When I use setTimeout I receive ReferenceError: setTimeout is not defined. Is this how it's supposed to be? Is it possible to somehow include this function?

I've installed V8 standalone and execute javascript code like this: ./d8 source.js. When I use setTimeout I receive ReferenceError: setTimeout is not defined. Is this how it's supposed to be? Is it possible to somehow include this function?

Share Improve this question asked Sep 8, 2012 at 23:12 renren 3,9939 gold badges53 silver badges100 bronze badges 2
  • I've never run V8 like this, but my intuition is that there is no window object, from which setTimeout would normally be called. – Jared Farrish Commented Sep 8, 2012 at 23:17
  • @JaredFarrish Your intuition is right and also wrong - everything is on window when the browser is the host environment, not just calls to browser APIs. – user11390576 Commented Oct 25, 2019 at 21:42
Add a comment  | 

2 Answers 2

Reset to default 13

setTimeout is not part of ECMA-262, it's implemented by the browsers. However, if you install Node.js (which is V8 + extras) you will get a command line setTimeout.

For what it's worth, V8 has its own setTimeout now (~7.5 years later), in the shell it provides. But it only takes one parameter (the function to call) and schedules it to be called once the current job is completed, roughly as though you'd passed 0 as the second parameter to the more familiar form of setTiemout provided by browsers and Node.js.

So given example.js:

console.log("a");
setTimeout(() => {
    console.log("c");
}, 5000);
console.log("b");

then

$ v8 example.js

outputs

a
b
c

...with no appreciable delay between b and c.

(That example uses the v8 command installed by jsvu, which is at least one way you run code directly in V8. I think d8 got subsumed...)

发布评论

评论列表(0)

  1. 暂无评论