JavaScript-primer

Timer and Intervals in JavaScript

setInterval

The setInterval() function is used to execute a function or block of code repeatedly, at a specified interval.

Here is an example to simulate a student’s increasing knowledge every day

let knowledge = 0;

function increaseKnowledge() {
  knowledge++;
  console.log(`Student's knowledge has increased to ${knowledge}.`);
}

setInterval(increaseKnowledge, 86400000); // Execute every 24 hours (86400000 milliseconds)

In this example, the increaseKnowledge() function is passed to setInterval() as a callback function, and is executed every 24 hours (86400000 milliseconds). The knowledge variable is incremented on each iteration, and a message is logged to the console indicating the student’s increased knowledge.

clearInterval():

You can use clearInterval() to stop the timer and stop the student’s knowledge from increasing. For example:

let timerId = setInterval(increaseKnowledge, 86400000);

// Stop the timer after a certain amount of time
setTimeout(function() {
  clearInterval(timerId);
  console.log('Timer stopped.');
}, 1000000);

In this example, the timer is stopped after 1 million milliseconds (about 11.6 days). You can adjust the delay and interval values to suit your needs.

setTimeout

The setTimeout() function is used to execute a function or block of code after a specified delay


let count = 0;

function incrementCounter() {
  count++;
  console.log(count);
  setTimeout(incrementCounter, 1000);
}

setTimeout(incrementCounter, 1000);
// Output: 1 (after a 1 second delay)
//         2 (after a 1 second delay)
//         3 (after a 1 second delay)
//         ...


In this example, the incrementCounter() function is passed to setTimeout() as a callback function, and is executed every 1 second. The count variable is incremented and logged to the console on each iteration.

requestAnimationFrame

The requestAnimationFrame() function is used to schedule a function or block of code to be executed before the browser’s next repaint. It allows you to animate elements on a web page by repeatedly executing a function or block of code, while ensuring that the animation runs smoothly and efficiently.

Here is an example of using requestAnimationFrame() to animate a div element:

const div = document.getElementById('animated-div');
let x = 0;

function animate() {
  div.style.left = x + 'px';
  x++;
  requestAnimationFrame(animate);
}

requestAnimationFrame(animate);

In this example, the animate() function is passed to requestAnimationFrame() as a callback function, and is executed before each repaint. It updates the left style property of the div element, causing it to move horizontally across the screen.

The requestAnimationFrame() function returns an ID that can be used to cancel the animation using the cancelAnimationFrame() function. For example:

let animationId = requestAnimationFrame(animate);

// Stop the animation after a certain amount of time
setTimeout(function() {
  cancelAnimationFrame(animationId);
  console.log('Animation stopped.');
}, 1000);

In this example, the animation is stopped after 1 second (1000 milliseconds). You can adjust the delay value to suit your needs.

The requestAnimationFrame() function is more efficient than using setTimeout() or setInterval() for animating elements, because it allows the browser to optimize the rendering of the animation and to synchronize it with the refresh rate of the display. This can result in smoother, more fluid animations and can help to improve the performance of your web applications.