Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using window.open inside setInterval()

Tags:

javascript

I want to create a timer that will reload extensions every 10 minutes.

This works, except the window doesn't open. I tried it with www.google.com as well

any thoughts?

var count = 0

var jsUpdatePageTimer = setInterval(updatePage, 5000)

function updatePage(){
    console.log(count)
    if (count == 3) {
        console.log('count is 3')
        clearInterval(jsUpdatePageTimer)
    } else {
        console.log('updating count')
        window.open('http://reload.extensions')
        count +=1 
    }
}
like image 653
Morgan Allen Avatar asked Mar 19 '26 17:03

Morgan Allen


1 Answers

popup blockers like to block calls to window.open — you may remember the terrible days when javascript could open popups willy-nilly

nowadays, for window.open to work, you have to use it within a user-initiated call stack

which means, you can only use window.open as the result of a user-triggered event, like a mouse click

button.onclick = () => {
  window.open(/*...*/)
}

so you need to ask yourself: what is the relevant user action that should trigger this popup? you need to have the user's consent

like image 131
ChaseMoskal Avatar answered Mar 21 '26 07:03

ChaseMoskal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!