Home>

While sending the file to the specified url with an event handler such as onclick,
I wanted to write a while statement to wait for the file to be selected and have the console display a message every second until the file path is greater than 0.
(At the end of the sentence
form.submit ();
form.remove ();
Will be included. )

The event handler (button onclick) stops responding when the button is pressed.

Corresponding source code

function upload (url) {
var form = document.createElement ("form");
var tmp = document.createElement ("input");
tmp.setAttribute ("type", "file");
form.appendChild (tmp);
form.method = "POST";
form.action = url;
document.body.appendChild (form);
tmp.click ();
while (tmp.value.length == 0) {
setTimeout (function () {
console.log ("1 second has passed")
}, 1000);
}

What I tried

-If i change the while statement to an if statement, the passage of 1 second will be displayed only once.
-It was confirmed that tmp.value.length is larger than 0 when normal and 0 when a file is specified.
-If only console.log is used in the while statement, it will work normally (although it will loop).

Supplementary information (FW/tool version, etc.)

The browser is a chrome browser.

  • Answer # 1

    To wait for processing until the file selection is completed

    I don't understand a little
    Should I use the change event with addEventListener for the file?