DEV Community

Cover image for Timing Functions in JS?
_Khojiakbar_
_Khojiakbar_

Posted on

Timing Functions in JS?

All the following functions(setTimeout, clearTimeout, setInterval, clearInterval) are part of JavaScript's timing functions. These functions are used to schedule the execution of code at specific times or intervals.

Summary of Timing Functions

  1. setTimeout: Schedules a one-time execution of a function after a specified delay.
  2. clearTimeout: Cancels a setTimeout if it hasn't already executed.
  3. setInterval: Schedules repeated execution of a function at specified intervals.
  4. clearInterval: Cancels a setInterval to stop the repeated execution.

Usage Example Combining All Timing Functions

Here's a combined example using all these timing functions in a single script:

// Schedule a one-time function with setTimeout
let snackTimeout = setTimeout(() => {
  console.log("Time for a snack!");
}, 5000);

// Schedule a repeated function with setInterval
let reminderInterval = setInterval(() => {
  console.log("Don't forget to drink water!");
}, 2000);

// Cancel the snack timeout before it executes
clearTimeout(snackTimeout);

// Cancel the reminder interval after 10 seconds
setTimeout(() => {
  clearInterval(reminderInterval);
  console.log("Stopping the water reminders.");
}, 10000);
Enter fullscreen mode Exit fullscreen mode

How They Work Together

  1. setTimeout schedules a message to be printed after 5 seconds but is canceled with clearTimeout before it can execute.
  2. setInterval schedules a message to be printed every 2 seconds.
  3. After 10 seconds, clearInterval stops the repeated messages from setInterval.

Word to Call All These Functions

You can refer to these functions collectively as timing functions or timer functions in JavaScript. They are essential tools for managing time-based events in web development.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay