0

I have a list of URLs in an Excel file. Each URL, when opened, changes, and I need to capture the new URL.

So I wrote some JS to open the file, get each cell, open a new tab with the URL, wait for it to change and grab the new URL, putting it into the cell again.

The problem is, the setTimeout instruction does not wait, so it grabs the same URL. Here's what I have:

for (var i = 0; i < excelRows.length; i++) {
var row = table.insertRow(-1);

/*This is just for me to test it with one URL*/
if (i < 2) {
            var tempTab = window.open(excelRows[i].URL); /*excelRows is defined prior to this block of code*/
            setTimeout(function(){excelRows[i].URL = tempTab.location.href;}, 3000);
            }

var cell = row.insertCell(-1);
cell.innerHTML = excelRows[i].URL;

}

I know, I'm supposed to close temptTab too, but I want to understand first why this executes immediately, if I'm using it asynchronous.

  • Besides the information about asynchronous calls, change var to let or const (especially in the loop). https://stackoverflow.com/questions/31285911/why-let-and-var-bindings-behave-differently-using-settimeout-function – Zachary Haber May 02 '20 at 16:45
  • `setTimeout` **does** wait. `cell.innerHTML =` does not (because you didn't put it in the function) – Quentin May 02 '20 at 16:46

0 Answers0