Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Make the `for` loop wait for each function to resolve

Page 1:

function tempWindow(text)
{
    var w = window.open('https://example.com/?data='+text, '_blank');
    var t = setInterval(() => {
        if(w.closed)
        {
            clearInterval(t);
            return true;
        }
    }, 100);
}

var list = ['text1', 'text2', 'text3'];

for(var i=0;i<list.length;i++)
{
    tempWindow(list[i]);//Each loop iteration must wait for each window to close
}

Temporary window:

(function(){
    setTimeout(() => {
        window.close();
    }, 5000);
})();

The code on page 1 should open three windows and send to each of them the strings text1, text2, text3, stored in the variable data.

These windows must not be opened at the same time. For this, setInterval checks every 100 milliseconds if the previous window has already been closed.

Therefore, the second window can only be opened after closing the first one and so on.

I believe that the tempWindow function could return a promise and that using await a solution would be possible, but I don't know how to do that.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

use that code. it will wait until window is close

function tempWindow(text) {
    return new Promise(resolve => {
        var w = window.open('https://example.com/?data=' + text, '_blank');
        var t = setInterval(() => {
            if (w.closed) {
                clearInterval(t);
                resolve(true);
            }
        }, 100);
    })
}

var list = ['text1', 'text2', 'text3'];

for (var i = 0; i < list.length; i++) {
    await tempWindow(list[i]);//Each loop iteration must wait for each window to close
}

make sure loop code is inside async function like that

async testFunction(){
    var list = ['text1', 'text2', 'text3'];

for (var i = 0; i < list.length; i++) {
    await tempWindow(list[i]);//Each loop iteration must wait for each window to close
}
}

call function like this testFunction()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!