js-10s to close the window after the countdown

effect:

 

html:

<h1 id = "h1"> Close after 10s </ h1 of>
<a id="a1"> start </a>

js:

 

(function () {
var S = 10;
var Timer = null;
a1.onclick = function () {
// To prevent continuous click button, causes the timer is performed too fast, add determines
if (timer == null) when none {// timer is running, in order to start the timer.
timer = the setInterval (function () {
S - Canton;
// like the countdown to zero, is stopped
if (s> 0) {// > = 0
h1.innerHTML = `$ {s} s window is closed automatically`;
} {the else
the clearInterval (Timer);
Timer = null;
this.innerHTML = "start"
}
}, 1000)
this.innerHTML = "stop"
} {the else // If the timer has run, click the button is to stop
clearInterval (the timer);
the timer = null;
this.innerHTML = "start"
}
}

})()

Guess you like

Origin www.cnblogs.com/snowbxb/p/12021261.html