首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ajax调用不应等待响应

Ajax调用不应等待响应
EN

Stack Overflow用户
提问于 2011-07-05 21:49:39
回答 2查看 11.6K关注 0票数 3

我在我的经典asp应用程序中使用ajax call来执行一个存储过程。

但是我不想等到存储过程开始运行(存储过程大约需要5-10分钟才能完成)。

Ajax必须调用存储过程,并且需要立即返回。

我希望Ajax调用不应该等待响应。

下面是我的代码片段:

代码语言:javascript
运行
复制
1)  $.ajax({  
  type: "POST",  
  url: "runstoredprocedure.asp",  
});    
2)  setInterval(function(){  jQuery("#list").trigger("reloadGrid"); },10000);  

这是我正在使用的两个ajax调用。第一个大约运行5-7分钟。在第一个完成之前,第二个不会触发。但我需要立即调用第二个ajax调用。

在这个问题上,有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-05 21:51:57

默认情况下,AJAX是异步的(这也是所有javascript库中的默认选项)。例如,在jQuery中:

代码语言:javascript
运行
复制
$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

你成功了,这需要一个回调。当您的操作完成时,将调用回调。jQuery会立即返回。

票数 2
EN

Stack Overflow用户

发布于 2011-11-28 14:40:07

javascript将请求作为另一个线程的一部分发出,ajax调用之后的任何代码都将立即执行。尽管如此,关于JS async的一个误解:

代码语言:javascript
运行
复制
People take for granted that because it’s asynchronous, it’s a thread. They are partially right. There must be a thread created by the browser to keep the javascript running while it makes a request to the server. It’s internal and you don’t have access to that thread. But, the callback function called when the server responds to the ajax request is not in a thread.

I’ll explain clearer. If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called). That’s because javascript itself doesn’t create a thread to execute the ajax response from the server and simply waits that all executions are terminated before starting a new one.

So if you’re running a lot of ajax requests simultaneously, you might get some weird behavior because they will all wait one on another before executing themselves.

最后一句话与你的事业有关。

博客摘录:http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/

有趣的阅读:http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6583708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档