如果Javascript不是多线程的,那么是否有任何理由实现异步Ajax队列? [英] If Javascript is not multithreaded, is there any reason to implement asynchronous Ajax Queuing?

查看:97
本文介绍了如果Javascript不是多线程的,那么是否有任何理由实现异步Ajax队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php服务器出现问题(我的计算机是唯一的连接).最初,我认为部分原因是由于ajax请求过多(我有一个脚本会在每次击键时执行ajax请求),所以我实现了一种设计,以控制ajax请求进入队列的流程.下面是我的代码:

I am having issues with my php server (my computer is the only connection). I initially thought part of the reason was because of too many ajax requests (I have a script that executes an ajax request per keystroke) so I implemented a design to control the flow of ajax requests into a queue. Below is my code:

//global vars:
activeAjaxThread = 0; //var describing ajax thread state
ajaxQue = [];  //array of ajax request objects in queue to be fired after the completion of the previous request

function queRequest(ajaxObj) {
  ajaxQue.push(ajaxObj);
  if (activeAjaxThread == 0) {
    fireOffAjaxQue();   
  } else {
    return 'ajax thread is running';
  }
}

function fireOffAjaxQue () {
  activeAjaxThread = 1;
  //getLastRequest();
  if ((ajaxQue.length > 0) && activeAjaxThread == 1) {
    $.ajax(ajaxQue[0]).always( function () {
      ajaxQue.shift(); 
      if (ajaxQue.length > 0) {
        fireOffAjaxQue();   //fire off another ajax request since this one has been completed. 
      }
    });
  }
  activeAjaxThread = 0;   //thread has finished executing
}

实施:

//create ajax object
var ajaxObj = {
  url: 'someplace.php',
  data: dataVar,
  success: function (data) {...}
};
//send ajax object to que
queRequest(ajaxObj);

然后我发现Javascript是多线程的,并阅读了一些有关Javascript事件处理的文章,例如

Then I found out that Javascript is multi-threaded and read a few articles on Javascript's event handling, such as this on http://ejohn.org/blog/how-javascript-timers-work/ by John Resig (jQuery extra-ordinaire)

既然是这样,由于Javascript已经对我的请求进行了排队,我在这里介绍的功能不会产生任何结果吗?奇怪的是,它似乎彻底改变了它.我的服务器崩溃的次数更少((不是那在任何想象中的解决方案 ... argh),并且在崩溃并重新启动后,前一个排队的ajax请求被发送,更早的时候似乎所有请求都被立即发送,并在服务器崩溃时消失了.

That being the case, wouldn't the functions I've presented here produce no results since Javascript is already queuing my requests? The weird thing is that it appears to change it drastically. My server crashes less, (not that that deems it a solution in any stretch of the imagination... argh), and after it crashes and restarts, the previous queued ajax requests get sent, when earlier they seemed to all be sent at once and disappear into thin air on server crash.

如果Javascript是单线程的,则将异步事件排队:

If Javascript, being single-threaded, queues asychronous events:

  1. 是否有必要使用Ajax请求管理器或队列?
  2. 为什么我的代码根本不会产生任何结果?

推荐答案

JavaScript是单线程的,但是可以说,一旦发送了AJAX请求,它就不再是javascript的手了.那就是AJAX的异步部分.发出请求,然后代码继续前进.然后,当收到响应时,将对其进行处理.请求没有排队.

Javascript is single threaded, but once the AJAX request is sent, it is out of javascript's hands, so to speak. That's the Asynchronous part of AJAX. The request is made and then the code moves on. Then when the response is received it is processed. Requests are not queued.

这篇关于如果Javascript不是多线程的,那么是否有任何理由实现异步Ajax队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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