简体   繁体   English

Js进度在get()或load()之后停止

[英]Js Progress stop after get() or load()

I have a click function will load page first and then continue the remaining process. 我有一个点击功能将首先加载页面,然后继续剩余的过程。

My problem is on the FIRST click, "Continue Here" will stop after $.get() function. 我的问题是第一次点击,“继续这里”将在$.get()函数后停止。

However, on the SECOND click it will work fine. 但是,在SECOND点击它会工作正常。

.click(){
  $.get(url, function(data){ content = data; } );

  // Continue here
  var x = content or etc...

}

Does anyone have idea about this? 有没有人对此有所了解? I am sure the problem is from the ajax loading, do I have a way to escape this? 我确定问题来自ajax加载,我有办法逃避这个吗? Thank you for your advice! 感谢您的意见!

Get is asynchronous, you must put your code inside the function Get是异步的,你必须把你的代码放在函数中

.click(){
  $.get(url, function(data){ 
    var content = data; 

    // Continue here
    var x = content; // or etc...
  });
}

You didn't post much code, but I'm guessing the issue is due to your get call which is asynchronous. 你没有发布很多代码,但我猜这个问题是由于你的get调用是异步的。 Therefore your variable content doesn't exist yet. 因此,您的变量content尚不存在。

.click(){
  $.get(url, function(data){ 
        content = data; } 
 );

 // Continue here
 var x = content; 

 //but content isn't anything yet because GET has not yet returned from the server. 
 //By the time you click a second time, the data has finally round tripped.

You have to do X in your callback instead. 你必须在回调中做X.

.click(){
   $.get(url, function(data){ 
        content = data; 
        var x = content;
    } 
);

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

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