0

So I have a simple function that executes 2 forms one after the other and then I would want the page I am on to wait for 3 seconds before opening the page example.com in the same window.

Given the function below, how would I have to do it. I've tried setting the line to "setTimeout("window.open('http://example.com','_self')",3000)" but that didn't work...

function SubmitBoth()
{
document.frm1.submit();
document.frm2.submit();
window.open('http://example.com','_self');
}

I've tried setting the line to "setTimeout("window.open('http://example.com','_self')",3000)" but that didn't work.

  • `that didn't work` What happened exactly? Can you open the browser Console and see any logs when the code is executing, and add that here. – ryanwebjackson Aug 10 '23 at 16:58

1 Answers1

0

Im a little unsure as to the reason behind wanting to implement this delay, it does not seem like best practice to me. Nonetheless I suggest this async/await solution, ref this Stack Overflow answer:

const delay = ms => new Promise(res => setTimeout(res, ms));

async function submitForms() {
    document.frm1.submit();
    document.frm2.submit();
    await delay(3000).then(_ => {
        window.open('http://example.com','_self');
    });
}