Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
纯JavaScript异步/等待在while循环中等待每个XMLHttpRequest完成,然后再发送下一个XMLHttpRequest_Javascript_Loops_Async Await_Xmlhttprequest - Fatal编程技术网

纯JavaScript异步/等待在while循环中等待每个XMLHttpRequest完成,然后再发送下一个XMLHttpRequest

纯JavaScript异步/等待在while循环中等待每个XMLHttpRequest完成,然后再发送下一个XMLHttpRequest,javascript,loops,async-await,xmlhttprequest,Javascript,Loops,Async Await,Xmlhttprequest,我试图用一个异步/等待片段更新几年前的一个项目。我认为这不是今天写它的正确方式(fetch等),但它是这样写的吗?这是在防火墙后面。按下HTML按钮时,将调用函数btnCurrent2Machine。这首曲子我真的很费劲。这段代码没有显示我在async/await上的任何尝试。然而,我做了很多尝试。谢谢 // Function to make a XMLHttpRequest to transfer a recipe // from a central host to a particular

我试图用一个异步/等待片段更新几年前的一个项目。我认为这不是今天写它的正确方式(fetch等),但它是这样写的吗?这是在防火墙后面。按下HTML按钮时,将调用函数btnCurrent2Machine。这首曲子我真的很费劲。这段代码没有显示我在async/await上的任何尝试。然而,我做了很多尝试。谢谢

// Function to make a XMLHttpRequest to transfer a recipe
// from a central host to a particular machine
function btnCurrent2Machine( tool )
{
  // Defines variable to hold "select" element
  var valuesCurrent = document.getElementById( "valuesCurrent" );

  // If nothing selected give error
  if( valuesCurrent.selectedIndex == -1 )
  {
    window.alert( "You must first select item(s) from the \"Current\" pane." )
  }
  else
  {
    // Loop through all options
    for( var option = 0; option < valuesCurrent.length; option++ )
    {
      if( valuesCurrent[option].selected == true )
      {
        // Option was selected
        (function (optionValue)
        {
          // Setup request
          var xhttp = new XMLHttpRequest( );

          xhttp.onreadystatechange = function( )
          {
            if( xhttp.readyState == 4 && xhttp.status == 200 )
            {
              // Response is back from server
              if( xhttp.responseText == "" )
              {
                // Response is good
                // Find index
                var i = 0;
                for( i = 0; i < valuesCurrent.length; i++ )
                {
                  if( optionValue == valuesCurrent[i].value )
                  {
                    indexCurrent = i;
                    break;
                  }
                }

                // Find index
                var indexMachine = 0;
                for( i = 0; i < valuesMachine.length; i++ )
                {
                  if( optionValue == valuesMachine[i].value )
                  {
                    indexMachine = i;
                    break;
                  }
                }

                // If not already in select for that machine create option for recipe
                if( !indexMachine )
                {
                  var optionClone = valuesCurrent[indexCurrent].cloneNode( true );

                  valuesMachine.appendChild( optionClone );
                }

                // Show in message that recipe transferred.
                // Above the option was appended to the bottom of the list
                document.getElementById( "statusMachine" ).innerHTML = valuesCurrent[indexCurrent].value;
              }
              else
              {
                // Display error message from server
                alert( xhttp.responseText );
                return 1;
              }
            }
          };

          // Open/send call to server
          xhttp.open( "GET", "current2machine.php?tool=" + tool + "&recipe=" + valuesCurrent[option].value, true );
          xhttp.send( );
        })(valuesCurrent[option].value);
      }
    }
  }
}
//用于生成XMLHttpRequest以传输配方的函数
//从中央主机到特定计算机
功能BTN当前2机床(刀具)
{
//定义用于保存“select”元素的变量
var valuesCurrent=document.getElementById(“valuesCurrent”);
//如果未选择任何内容,则给出错误
如果(值current.selectedIndex==-1)
{
提醒(“您必须首先从“当前”窗格中选择项。”)
}
其他的
{
//循环浏览所有选项
对于(var option=0;option
不能将async/Wait直接用于XmlHttpRequest。async/await只适用于返回承诺的东西,而不是。您需要切换到使用fetch。(或者,您可以在自己构建的承诺中封装一堆XHR代码,但是相信我,使用fetch是一个更简单的解决方案。)非常感谢您的帮助。对不起,我花了这么长时间。我刚发完这封信,我们的商店就爆炸了。