Javascript执行顺序 - 当window.location.href发生时? [英] Javascript Execution Order - When window.location.href happens?

查看:896
本文介绍了Javascript执行顺序 - 当window.location.href发生时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这个 is-javascript-location-href-call -is-asynchronous 无论如何,事件循环到底是什么但是没有解释 href 的定义行为window.location



以下是源代码中的脚本顺序:



1。 DOM元素



2。 <脚本>在底部有doc.ready。



3.Further< script>标签。



4。 doc.ready的回调进入CB队列,因为所有DOM元素在步骤1中都可用,并且只有其他脚本标记(此回调保留在队列中,直到步骤3中的所有脚本完成其执行并且清空调用堆栈。正确吗?



5。 window.location.href = /同步AJAX的fail()内的错误。它不会立即重定向。所以它是异步的。



//这个fail()方法中的同步等待方法〜> = 0.5秒紧接着(用于测试) [标签:1]



6。调用堆栈在这里变空,因为没有更多的脚本来执行



问题:如果 href 也表现为事件/ XHR /超时, href 步骤5 进入事件队列?如果是这样,那么何时? (事件,AJAX,setTimeout已定义进入队列的行为,但 href 重定向怎么办?)



实验:如果同步等待是在步骤5 之后,那么 href 重定向发生的时代。但是有时 doc.ready 的回调也会被执行。



问题:如果 href 的行为类似于事件/ XHR /超时,那么它始终是 doc.ready之后队列中的第二个,即使使用同步等待,也应该始终在 doc.ready 之后执行。但它没有基于上述实验!!所以它显示 href 不涉及事件队列,对吗?那么,当其他事件/ XHR /超时出现并与Call Stack或Event / CB Queue相关时, href 的定义行为是什么?

解决方案

停止。 研究Javascript事件循环。 JS执行代码。当遇到事件 - 通常是异步调用时 - 它被推送到事件队列并且JS执行继续进行。事件队列是先进先出。在任何时候都无法确定队列中的内容,因此任何给定事件的执行 - 你的href - 是不可预测的 - 它不是偶然的。如果程序执行是偶然,那么计算机甚至都不会工作。 / p>

I took a look at this is-javascript-location-href-call-is-asynchronous and this what the heck is the event loop anyway but there is no explanation of the defined behavior of href of window.location:

The following is the script order in source code:

1. DOM elements

2. <script> at the bottom having doc.ready.

3.Further <script> tags.

4. doc.ready's callback goes into the CB queue because all DOM elements are available in step 1 and there are only further script tags ( This callback stays in the queue until all the scripts in step 3 complete its execution and empties the call stack. Correct? )

5. window.location.href=/error inside the fail() of sync AJAX. It does not immediately redirect. So it is async.

// synchronous wait method ~ >= 0.5sec inside this fail() method just after (for testing) [TAG: 1]

6. call stack becomes empty here as there no more scripts to exec

Question: If href also acts like Events/XHR/Timeouts, does href at step 5 go into the event queue? If so, exactly when ? ( Events, AJAX, setTimeouts have defined behavior of when it gets into the queue but what about href redirection? )

Experiment: If the synchronous wait is there after step 5, then href redirection happens "most" of the times. But "sometimes" doc.ready's callback is also executed.

Question: If href acts like Event/XHR/Timeouts, then it is always the second in the queue after doc.ready and so even with the synchronous wait, it should always execute after doc.ready. But it did not based on the above experiment!! So it shows href does not involve event queues, right? Then, what is the defined behavior of href when other Events/XHR/Timeouts are around and relate to Call Stack or Event/CB Queue ?

解决方案

Stop. Research Javascript Event Loop. JS executes code linerly. When an event - asynchronous call generally speaking - is encountered it is pushed to an "event queue" and JS execution keeps going. The event queue is 'first in, first out'. There's no telling what is in the queue at any moment so the execution of any given event - your href - is unpredictable - it is not 'by chance.' If program execution was 'by chance', then computers would not even work.

这篇关于Javascript执行顺序 - 当window.location.href发生时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆