简体   繁体   English

JavaScript location.href 调用是异步的吗?

[英]Is JavaScript location.href call asynchronous?

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??");
}

In the above lines of code the location.href is getting called before console.log("Hello, how are you") , alert and fun1() .在上面的代码行中, location.hrefconsole.log("Hello, how are you")alertfun1()之前被调用。

When I call the fun() it executes all the statements below location.href and then it redirects to https://www.google.com .当我调用fun()它会执行location.href下面的所有语句,然后重定向到https://www.google.com Is the location.href call asynchronous in nature, and if not then what is happening? location.href调用本质上是异步的,如果不是,那么发生了什么?

I thought the moment it redirects the user to other page, the lines of code below it would never execute.我认为当它将用户重定向到其他页面时,它下面的代码行永远不会执行。

Any help/explanation is appreciated!任何帮助/解释表示赞赏!

A browser will execute code after window.location.href = "http://google.com" until the browser goes to the next web address.浏览器将在window.location.href = "http://google.com"之后执行代码,直到浏览器转到下一个网址。 As such, the number of lines that will be executed depends on some combination of the browser's speed or later synchronous input from the user (an alert in your case).因此,将执行的行数取决于浏览器速度或用户稍后的同步输入(在您的情况下为alert )的某种组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM