问题描述
function fun(){
console.log("Hi");
window.location.href="http://www.google.com";
console.log("Hello, how are you");
alert("I am good");
fun1();
}
function fun1(){
console.log("Whats up??");
}
如果您看到上面的代码行位置.href
在
console.log(你好,你好吗),alert和fun1()之前被调用。
If you see the above lines of code the location.href
is getting called beforeconsole.log("Hello, how are you"), alert and fun1().
当我调用 fun()
时,它执行 location.href
下的所有语句,然后重定向到 https://www.google.com
。
when I call the fun()
it executes all the statements below location.href
and then it redirects to https://www.google.com
.
所以我的问题是,位置.href
调用本质上是异步的,如果没有,那么这里发生了什么??
So my question is , "Is location.href
call is asynchronous in nature, if not then what is happening over here" ??
因为我想当下它会重定向用户到其他页面,它下面的代码行将永远不会执行。
Because I thought the moment it will redirect the user to other page, the lines of code below it will never execute.
感谢任何帮助/解释!!!
Any help/explanation is appreciated!!!
谢谢
推荐答案
浏览器将在 window.location.href ='http之后执行代码://google.com
,直到浏览器转到下一个网址。因此,将执行的行数取决于浏览器速度的某种组合或用户稍后的同步输入(在您的情况下为 alert
)。
A browser will execute code after window.location.href = 'http://google.com
until the browser goes to the next web address. As such, the number of lines that will be executed depends on some combination of the browsers speed or later synchronous input from the user (an alert
in your case).
这篇关于JavaScript location.href调用是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!