Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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等待函数_Javascript_Function_Settimeout_Delay_Synchronous - Fatal编程技术网

JavaScript等待函数

JavaScript等待函数,javascript,function,settimeout,delay,synchronous,Javascript,Function,Settimeout,Delay,Synchronous,我在JavaScript中遇到了一个小问题。我需要停止代码,暂时什么也不做。我尝试了setTimeout,但它只安排函数,继续执行代码,然后返回。我真的需要等待用户在输入字段中输入一些值,然后按下按钮。代码开头的sleep函数可以正常工作,但代码不知何故停止显示html输入表单和按钮。我不明白为什么。我也不能在提交按钮上使用onclick属性,因为同样的问题。有人知道这里有什么问题吗 var returning = 0; // variable for deciding which part o

我在JavaScript中遇到了一个小问题。我需要停止代码,暂时什么也不做。我尝试了setTimeout,但它只安排函数,继续执行代码,然后返回。我真的需要等待用户在输入字段中输入一些值,然后按下按钮。代码开头的sleep函数可以正常工作,但代码不知何故停止显示html输入表单和按钮。我不明白为什么。我也不能在提交按钮上使用onclick属性,因为同样的问题。有人知道这里有什么问题吗

var returning = 0; // variable for deciding which part of function to use 

    function sleep(milliseconds) { // sleep method found here on stackoverflow
        var start = new Date().getTime();
        for (var i = 0; i < 1e7; i++) {
          if ((new Date().getTime() - start) > milliseconds){
            break;
          }
        }
      }


    function acceptValue(){
        // show an input field with button next to it
        if (returning == 0) {
             var co = document.createElement("input"); // show input field and set attributes   
             var kam = document.getElementById("telo");   
             var indexId = 0;
             while(document.getElementById("pole" + indexId) != null) {
                 indexId++; // look for closest unused id name
             }
             co.setAttribute("id", "pole" + indexId);     
             kam.appendChild(co);

             var co1 = document.createElement("input");
             var kam1 = document.getElementById("telo");

             var indexId1 = 0;
             while(document.getElementById("cudlik" + indexId1) != null) {
                 indexId1++; // look for closest unused id name
             }
             co1.setAttribute("id", "cudlik" + indexId1); // show button and set attributes
             co1.setAttribute("type", "submit");
             co1.setAttribute("value", ">");
             co1.setAttribute("onclick", "vraceni()");
             kam1.appendChild(co1); 

             console.log(document);
             document.getElementById("telo").appendChild(document.createElement("br"));
             returning = 1;
             acceptValue();

            } else if (vrat == 1) {   
                sleep(500);
                acceptValue();
                }
                

            }  else {
                var indexPole = 0;
                while (document.getElementById("pole" + indexPole) != null) {
                    indexPole++;
                }
                vrat = 0;
                return document.getElementById("pole" + (indexPole - 1)).value; // return the value from last shown input field

            } 

    }
    
    function returnFunc() {
      vrat = 2; // called from html button
    }

谢谢


Adam Hendrych

我认为您试图在这里创建的功能可能需要重新设计。有了这些while循环和睡眠技巧,感觉非常奇怪

您所描述的希望此代码执行的基本上是输入和按钮工作方式的默认行为。一个简单的表单包含一个input和submit按钮,表单上有一个onsubmit处理程序,它应该满足接受输入并在按下按钮时启动操作的要求


根据设计,Javascript是单线程和非阻塞的。“按设计等待”是指在等待间隔过期时触发事件,并分配回调来服务该事件。不要想一个,就像你已经实现的那样!在这里查看更多细节/好的建议:@palsm4 JavaScript不是真正的单线程。例如,浏览器肯定会在多个线程上执行。考虑到ECMAScript规范从未真正要求这一点,从设计上讲这是一种误导。此外,规范定义了专门用于处理和编码多线程执行的抽象。好的,我理解尝试一些睡眠方法不是一个好的解决方案。但不知道如何解决它。我还有另一部分代码,它依赖于这个函数的返回值。我需要等待用户给它一些值才能返回。但既然我不能这样做,那么什么是正确的解决方案??