<tfoot id='mgzmn'></tfoot>

      <i id='mgzmn'><tr id='mgzmn'><dt id='mgzmn'><q id='mgzmn'><span id='mgzmn'><b id='mgzmn'><form id='mgzmn'><ins id='mgzmn'></ins><ul id='mgzmn'></ul><sub id='mgzmn'></sub></form><legend id='mgzmn'></legend><bdo id='mgzmn'><pre id='mgzmn'><center id='mgzmn'></center></pre></bdo></b><th id='mgzmn'></th></span></q></dt></tr></i><div id='mgzmn'><tfoot id='mgzmn'></tfoot><dl id='mgzmn'><fieldset id='mgzmn'></fieldset></dl></div>
    1. <small id='mgzmn'></small><noframes id='mgzmn'>

        <bdo id='mgzmn'></bdo><ul id='mgzmn'></ul>

      <legend id='mgzmn'><style id='mgzmn'><dir id='mgzmn'><q id='mgzmn'></q></dir></style></legend>

    2. 如果 Javascript 不是多线程的,是否有任何理由实现异步 Ajax Queuing?

      时间:2023-09-29
      <i id='J4sSl'><tr id='J4sSl'><dt id='J4sSl'><q id='J4sSl'><span id='J4sSl'><b id='J4sSl'><form id='J4sSl'><ins id='J4sSl'></ins><ul id='J4sSl'></ul><sub id='J4sSl'></sub></form><legend id='J4sSl'></legend><bdo id='J4sSl'><pre id='J4sSl'><center id='J4sSl'></center></pre></bdo></b><th id='J4sSl'></th></span></q></dt></tr></i><div id='J4sSl'><tfoot id='J4sSl'></tfoot><dl id='J4sSl'><fieldset id='J4sSl'></fieldset></dl></div>
    3. <small id='J4sSl'></small><noframes id='J4sSl'>

        <legend id='J4sSl'><style id='J4sSl'><dir id='J4sSl'><q id='J4sSl'></q></dir></style></legend>

        <tfoot id='J4sSl'></tfoot>
            <tbody id='J4sSl'></tbody>
              <bdo id='J4sSl'></bdo><ul id='J4sSl'></ul>

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

                问题描述

                我的 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 事件处理的文章,例如 http://ejohn.org/blog/how-javascript-timers-work/ 作者:John Resig(jQuery extra-ordinaire)

                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 已经在对我的请求进行排队了吗?奇怪的是,它似乎彻底改变了它.我的服务器崩溃更少,(任何想象力中的并不是认为它是一个解决方案...啊),并且在它崩溃并重新启动后,之前排队的 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 Queuing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何将 javascript 函数存储在队列中以便最终执行 下一篇:获取最新的 ajax 请求并中止其他请求

                相关文章

                <small id='SiRLN'></small><noframes id='SiRLN'>

                      <bdo id='SiRLN'></bdo><ul id='SiRLN'></ul>
                    <legend id='SiRLN'><style id='SiRLN'><dir id='SiRLN'><q id='SiRLN'></q></dir></style></legend>
                    <i id='SiRLN'><tr id='SiRLN'><dt id='SiRLN'><q id='SiRLN'><span id='SiRLN'><b id='SiRLN'><form id='SiRLN'><ins id='SiRLN'></ins><ul id='SiRLN'></ul><sub id='SiRLN'></sub></form><legend id='SiRLN'></legend><bdo id='SiRLN'><pre id='SiRLN'><center id='SiRLN'></center></pre></bdo></b><th id='SiRLN'></th></span></q></dt></tr></i><div id='SiRLN'><tfoot id='SiRLN'></tfoot><dl id='SiRLN'><fieldset id='SiRLN'></fieldset></dl></div>
                  1. <tfoot id='SiRLN'></tfoot>