-4

I’m facing a small issue in JavaScript. I need to to make a code stop and do nothing for a while. I tried setTimeout, but it only scheludes the function, continues in executing the code and then comes back. I really need to wait for the user to put some value in the input field and then press the button. The sleep function on the beginning of my code works, but the code somehow stops showing my html input form and button. I can’t figure out why. I also can’t use onclick attribute on the submit button, because of the same problem. Does someone know what can be the problem here??

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
    }

Thanks,

Adam Hendrych

Papouc
  • 25
  • 7
  • 1
    By design, Javascript is *SINGLE THREADED* and *NON-BLOCKING*. By design "wait" means "trigger an event when your wait interval has expired, and assign a callback to service the event". Don't even *THINK* about a ["busy wait"](https://en.wikipedia.org/wiki/Busy_waiting), like you've implemented! Look here for more details/good suggestions: https://stackoverflow.com/a/39914235/421195 – paulsm4 Nov 11 '20 at 21:20
  • @paulsm4 JavaScript is not really "single threaded". A browser, for example, most definitely executes on more than one thread. And "by design" is a misleading, given that the ECMAScript specs never really require this. Moreover, the specs define abstract [agents](https://tc39.es/ecma262/#sec-agents) that are specifically to handle and codify multi-thread execution. – VLAZ Nov 11 '20 at 21:25

1 Answers1

-2

I think the feature you are attempting to create here may need some re-architecting. It feels very strange to have all these while loops and sleep tricks in place.

What you describe wanting this code to do is basically the default behavior of how inputs and buttons work. A simple form containing an input and submit button, with an onsubmit handler on the form, should meet the "accept input and fire an action when the button is pressed" requirement.

An example

  • Okay, I understand that trying to make some sleep() method is not a good solution. But do not know how to solve it. I have another part of code, that is dependent on returning value of this function. I need to wait for the user to give it some value in order to return in. But since I can’t make it this way, what is the right solution?? – Papouc Nov 12 '20 at 19:33