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

computer science - What is the term used to describe a complete call frame cycle thing in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

In JavaScript, there is the concept of the execution pathway beginning at a certain point (such as an event handler), with the control being relinquished back to the browser at some point.

Is there a proper name for this process?

Originally I thought you could refer to this as the "current Stack Frame", but after reading Wikipedia I see that refers to something else. It's not the call stack, that's the breadcrumb of where we've been.

(Apologies for the awkward title ... I'm open to suggestions :))

In JavaScript, there is the concept of the execution pathway beginning at a certain point (such as an event handler), with the control being relinquished back to the browser at some point.

Is there a proper name for this process?

Originally I thought you could refer to this as the "current Stack Frame", but after reading Wikipedia I see that refers to something else. It's not the call stack, that's the breadcrumb of where we've been.

(Apologies for the awkward title ... I'm open to suggestions :))

Share Improve this question asked May 15, 2015 at 18:59 Paul GordonPaul Gordon 2,6324 gold badges27 silver badges24 bronze badges 3
  • Do you mean the point at which there's no JavaScript actively running on the page? I doubt anyone has a name for this except the people who develop JS engines. – Chris Hayes Commented May 15, 2015 at 19:01
  • An Execution Context from the Execution Stack maybe? dmitrysoshnikov./ecmascript/chapter-1-execution-contexts – Sergiu Paraschiv Commented May 15, 2015 at 19:03
  • 1 I think you are just looking for the term Event Loop. – James Lawruk Commented May 15, 2015 at 19:10
Add a ment  | 

5 Answers 5

Reset to default 5

I've heard this called a "turn of the event loop" (by engineers at Mozilla and by members of TC39, the ECMAScript standard mittee).


In the ECMAScript standard, a Job is defined as "an abstract operation that initiates an ECMAScript putation when no other ECMAScript putation is currently in progress". That's exactly what you're looking for, but the term isn't widely used.

The HTML standard describes event loops in bewildering detail, but the term it uses, task, isn't widely used either.

I don't think there is a well-established, standard term for what you're asking about.

I've just heard it as "back to the event loop" or "finish the current thread of execution and return to the event loop".

And, node.js uses the term "next tick" to mean something similar.

Javascript (in both a browser and in node.js) is event driven and all asynchronous operations or user events get executed by putting an event in the event queue and that event gets processed only when the current thread of execution is done and the JS engine can then pull the next event off the event queue and start its execution. So, because the underlying mechanism is an event queue driven it is mon to use the phrase "event" in whatever term you use to describe it.

This article on Understanding process.nextTick() refers to "every tick of the event loop".

You can imagine this event loop to be a queue of callbacks that are processed by Node on every tick of the event loop.

The node.js documentation for process.nextTick() uses "event loop turn runs to pletion".

Once the current event loop turn runs to pletion, call the callback function.

The node.js documentation for setImmediate() refers to "event loop iteration".

Callbacks for immediates are queued in the order in which they were created. The entire callback queue is processed every event loop iteration. If you queue an immediate from inside an executing callback, that immediate won't fire until the next event loop iteration.

Is the term tick what you're looking for? In node.js (which is admittedly JavaScript), there's a function named process.nextTick() which accepts a callback function that's supposedly executed on the "tick" immediately after the current execution cycle. This is the term I've always used, but there could be other monly used terms to describe the event cycle of JavaScript.

The term you're looking for is the "event loop".

While one of the meanings of the event loop is the collection of "cycles" over the lifetime of the application, it also means simply "one cycle" - at least in English.

Grammatically, the rule for the usage is similar to the difference between "the road", which refers to the entire route taken on a journey and "a road", which refers to one segment of road among many segments which together make up "the road".

So, when talking about the architecture we call it "the event loop". And when talking about one cycle in this loop we simply say "a loop" or more specifically "one cycle of the event loop".

This terminology is old. It originated in GUI frameworks created in the late 70s and early 80s. Early developer documentation of the original Mac OS refers to it as the event loop (the Mac back then didn't have thread support, so like javascript the entire OS was single threaded).

The venerable Tk UI library which was released in the early 80s also refers to it as the event loop. Incidentally, the Tcl programming language, like javascript that came after it, es with a built-in event loop.

Traditionally the term used to hand control back to the event loop is "yield". In some languages and/or frameworks there is actually a keyword or function called "yield" to do this. In Tcl it's called "update" because that's the name of the function you'd call to deliberately enter the event loop from code.

Javascript doesn't have this feature* so there is no terminology in javascript programming for deliberately entering the event loop. The interpreter will simply enter the event loop where there is nothing else to run (basically when it reaches the end of all files).


*note: Technically you can indirectly cause code to suspend and enter the event loop in the browser. If you update the DOM then immediately query it (for example setting margin-width then querying for offsetWidth) the browser will enter the event loop to reflow the DOM. But this feature can't be used for anything else since the browser will only execute the reflow process and not trigger any other events.

You are referring to an empty "callback queue". This gets emptied by the "event loop" as mentioned in other answers.

Philip Roberts (@latentflip) gave a fantastic talk on this very topic at JSConf EU '14. Here's the video and transcript of his talk http://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-anyway.html. Great watch and will definitely leave you better understanding how javascript executes!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论