'Synchronize a general timer with functions to turn them off at the end of it

I am new here, also in Javascript. I would like to explain you my problem. I am currently making a JavaScript program consisting of a timer that, once it reaches 0, closes all the pages opened by the site. I managed to create the timer for the visual side, then the buttons to open the websites.

My problem is that I can't manage to synchronize a timer with the setTimeout of the winYt() functions etc... Because when I put 10s before the page closes, it's 10s from the moment the user click on the button. What I would like is that the pages close for example in 60s from the moment the global page opens, and therefore the timer starts, and that once the pages are opened with the buttons, they close only at the end of the timer.

I tried to link a timer function by putting it in a z variable to put it in the setTimeout, but it opens and closes the pages directly no matter what I try and what modification I try.

I also thought of making a timer that takes the exact time of when the site opens, then 5 minutes later, closes the pages (opened by the buttons only).

I come to ask you for help to find a function to make my pages close when the general timer reaches 0 only.

Thanks in advance,

Here's the general code:

document.getElementById("timer").style.color = "rgb(72, 0, 130)";
document.getElementById("timer").style.fontFamily = " Comic sans MS";
let temps = 10
const timerElement = document.getElementById("timer")



var X = 11200
var win = window.open("https://waytolearnx.com", "_blank", "width=auto, height=auto");
win.opener = window;
win.focus();
opener=self;
setTimeout("win.close()",X) 


function diminuerTemps() {
    let minutes = parseInt(temps / 60,10)
    let secondes = parseInt(temps % 60,10)
            
    minutes = minutes < 10 ? "0" + minutes : minutes
    secondes = secondes < 10 ? "0" + secondes : secondes
            
    timerElement.innerText = minutes + ":" + secondes
    temps = temps <= 0 ? 0 : temps -1
    
}
setInterval(diminuerTemps, 1000);

function winYt() {
    const Yt = window.open("https://www.youtube.com/", "_blank");
    setTimeout(function() {Yt.close()}, z);
}

function winKr() {
    const Kr = window.open("https://krunker.io/", "_blank");
    setTimeout(function() {Kr.close()}, z);
}

function winBk() {
    const Bk = window.open("https://bonk.io/", "_blank");
    setTimeout(function() {Bk.close()}, z);
}

function winSv() {
    const Sv = window.open("https://surviv.io/", "_blank");
    setTimeout(function() {Sv.close()}, z);
}

If you want, I can send you my test functions that I made

Have a nice day.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source