Skip to main content

How to create a timer in javascript


 To make a timer in javascript,we can use the following functions:

1. setTimeout

2. setInterval

setTimeout:

using set time out , we can trigger a specific function 'once' in a given amount of time.

for example:

    function myFunction() {

  console.log("Timer is done!");

}


// Set a timer to call myFunction after 2000 milliseconds (2 seconds)

const timerId = setTimeout(myFunction, 2000);


// You can cancel the timer before it executes using clearTimeout

// clearTimeout(timerId);

--------------------------------------------------------------------

setInterval:

using setInterval , we can trigger a function 'repeatedly'  with a break of specified time.

for example:

----------------------------------------------------------------------------------------------------------

function myFunction() {

  console.log("Timer is running!");

}


// Set a timer to call myFunction every 1000 milliseconds (1 second)

const timerId = setInterval(myFunction, 1000);


// To stop the interval, use clearInterval

// clearInterval(timerId);

------------------------------------------------------------------------------------------------------------------

Thanks for reading



Comments

Popular posts from this blog

when will spacex send people to mars ?

There was no specific timetable for SpaceX's ambitious intentions to carry humans to Mars as of my most recent information update in January 2022.  Elon Musk is the company's leader. Musk has proposed a crewed journey to Mars that might launch in the middle of the 2020s, but there are a number of technological, legal, and financial obstacles that could prevent this from happening.  The precise timescale for SpaceX's development of the Starship spaceship, a crucial element for Mars missions, is yet unknown. For the most recent details on SpaceX's intentions for Mars, check out the company's official website or the most recent news sources.

How to center a div both horizontally and vertically ?(with code)

 To center a div both horizontally and vertically, we use flex. if you want the full code , here it is : <! DOCTYPE html > < html lang = "en" > < head >     < style >         body {             margin : 0 ;             overflow : hidden ;         }         #centered {             width : 100 ;             height : 99vh ;             display : flex ;             justify-content : center ;             align-items : center ;         }     </ style >     < meta charset = "UTF-8" >     < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >     < title > yt </ title > </ ...

What would happen if a ceiling fan fall on you while it is turned on ?

 A ceiling fan falling while it is turned on can pose serious risks and potential hazards.  The outcome would depend on various factors, including the height from which the fan falls, the speed at which it is rotating, and the angle of impact. Here are some considerations: 1. **Blade Injuries:**    - Ceiling fans typically have multiple blades that can cause injuries if they come into contact with a person. The speed of the rotating blades can increase the severity of injuries. 2. **Impact Force:**    - The impact force from a falling ceiling fan, especially if it is turned on, can cause significant harm. The weight and speed of the falling fan can lead to injuries such as cuts, bruises, and fractures. 3. **Head Injuries:**    - If the falling fan strikes a person on the head or neck, there is a risk of head injuries, which can range from minor concussions to more severe traumatic injuries. 4. **Risk of Electrical Shock:**    - Since the...