[Learning]#28 JS: Timing Events — Part 2

Example 1. setTimeout

<body onload="init();">
<span id="timer">3</span>

<script type="text/javascript">
let timer;
function init(){
timer=document.getElementById("timer");
window.setTimeout(countdown, 1000);
}

function countdown (){
timer.innerHTML=timer.innerHTML-1;
if(timer.innerHTML>0){ //It will continue counting down until meeting 0.
window.setTimeout(countdown, 1000);
}else{
document.location="https://vidalin-777.medium.com/";
}
}

</script>
</body>

Example 2. setInterval

<script type="text/javascript">
let timer;
function init(){
timer=document.getElementById("timer");
window.setInterval(countdown, 1000);
}
function countdown (){
timer.innerHTML=timer.innerHTML-1;
if(timer.innerHTML>0){
}else{
document.location="https://vidalin-777.medium.com/";
}
}
</script>

Music of Today: Run by Phum Viphurit

Clap/Share/Follow

--

--

--

PM, Relationship, Travel, or anything quirky but interesting.|Subscribe & buy Vida a drink @LikerLand, the bottom of each article. 在文章底下LikerLand訂閱、請Vida喝一杯飲料 ❤

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Confident JS series: Part 4— A bit more functional

Don’t Use Switch or If-Else in JavaScript, Instead, Try This

You are running `create-react-app` 4.0.3,

How to write JavaScript with 6 characters

Worker Threads in Node JS

Redux-Ruleset devtools are out!

Creating a native animated driver for react-native-web: Traversing the graph and animating

Getting Started with NodeJS

Getting Started with NodeJS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vida Lin

Vida Lin

PM, Relationship, Travel, or anything quirky but interesting.|Subscribe & buy Vida a drink @LikerLand, the bottom of each article. 在文章底下LikerLand訂閱、請Vida喝一杯飲料 ❤

More from Medium

How to Fix JavaScript Runtime issues — Lazy Loading

call, apply and bind in JS

JS conceptual 2

How to use the includes() Array method on a Bootcamp API