How to Use Timers and the DOM in Object-Oriented JavaScript?

Estimated read time 1 min read

Here’s how to use timers and the DOM in object-oriented JavaScript:

  1. Timers: JavaScript has two timer functions – setTimeout and setInterval – that allow you to execute a function after a specified amount of time has passed.

Example:

// setTimeout
setTimeout(() => {
  console.log("Hello, World!");
}, 2000);

// setInterval
let count = 0;
const intervalId = setInterval(() => {
  count += 1;
  console.log(count);
  if (count === 5) {
    clearInterval(intervalId);
  }
}, 1000);
  1. DOM: The Document Object Model (DOM) is a programming interface for HTML and XML documents. You can use JavaScript to manipulate and interact with elements in the DOM.

Example:

// Select an element
let header = document.querySelector("header");

// Change its text content
header.textContent = "Welcome to my website";

// Add an event listener
header.addEventListener("click", () => {
  console.log("Header was clicked");
});

// Change its style
header.style.backgroundColor = "blue";
header.style.color = "white";

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply