samedi 2 octobre 2021

Is there a way to check whether a page is visible or not every N seconds, and then open a tab or execute another action? [closed]

I've managed to find a way to check whether a page is visible or not in JavaScript, but I cannot seem to make it actively check the visibility every N seconds, then output an alert (or some other action).

It doesn't seem to execute at all once I start trying to make it check every 5 seconds.

This was the only solution that executed at least once:

function onVisibilityChange(){
if(document.visibilityState == this.hidden == true){
    window.open("https://youtube.com")
}
else if(document.visibilityState == this.hidden == false){
    window.open("https://www.google.com");
}
}

setInterval(() => {
    document.addEventListener("visibilitychange", 
onVisibilityChange);
}, 5000);

Also, visible here means that the page isn't minimised or in the background.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire