1.setTimeout() //executes the code after x seconds.
2.setInterval() //executes the code **every** x seconds.
1var intervalID = setInterval(alert, 1000); // Will alert every second.
2// clearInterval(intervalID); // Will clear the timer.
3
4setTimeout(alert, 1000); // Will alert once, after a second.
5setInterval(function(){
6 console.log("Oooo Yeaaa!");
7}, 2000);//run this thang every 2 seconds
1// Set the date we're counting down to
2var start_at = new Date('2020-06-29 12:00:00');
3
4// Update the count down every 1 second
5var x = setInterval(function() {
6
7 // Get todays date and time
8 var now = new Date();
9
10 // Find the distance between now an the count down date
11 var distance = now - start_at;
12
13
14 // Time calculations for days, hours, minutes and seconds
15 var days = Math.floor(distance / (1000 * 60 * 60 * 24));
16 var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
17 var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
18 var seconds = Math.floor((distance % (1000 * 60)) / 1000);
19
20 // Output the result in an element with id="demo"
21 document.getElementById("demo").value = hours + "h "
22 + minutes + "m " + seconds + "s ";
23}, 1000);
1setTimeout() //executes the code after x seconds.
2.setInterval() //executes the code **every** x seconds.
1/*The difference between setTimeout() and setInterval() is that
2setTimeout() only executes code once, but setInterval() executes
3the code each amount of time you input. For example,*/
4
5/*In this function, it executes a function after 1000 milliseconds, or 1
6second.*/
7function oneSecond(){
8 setTimeout(1000, function(){
9 console.log('That was 1 second.')
10 })
11}
12
13/*In this function, it executes a function every 1 second*/
14function stopWatch(){
15 var count = 0;
16 setInterval(1000, function(){
17 console.log(count + " Seconds passed")
18 })
19}
1var intervalID = scope.setInterval(func, [delay, arg1, arg2, ...]);
2var intervalID = scope.setInterval(function[, delay]);
3var intervalID = scope.setInterval(code, [delay]);
4