The power of setInterval() and clearInterval()

Photo by Ocean Ng on Unsplash

setInterval() Method

function clock() {
return new Date().toLocaleTimeString()
}
setInterval(clock, 1000);

clearInterval() Method

Wrong
clearInterval( setInterval(function, 1000) )

Correct
const VAR = setInterval(function(), 1000)
clearInterval(VAR)

Why create the extra step?

This is PBV, there are 2 different copy of the setInterval.
setInterval(function, 1000)
clearInterval( setInterval(function, 1000) )

This is PBR, clearInterval is referring back to the setInterval
const VAR = setInterval(function(), 1000)
clearInterval(VAR)

Run one time

A mini project

--

--

--

Copy and Paste what’s on my mind. Perfect.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Some Tricky Things In JavaScript

Vue.js : Answering the Why and How !

E2E type safety with tRPC

Extending the OpenTelemetry Collector with your own components

A Modern Gmail Add-on Build Process

A simpler way to use IndexedDB

Another week of pushing through!

Google Ends Long-Term Support for Angularjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dong Xia

Dong Xia

Copy and Paste what’s on my mind. Perfect.

More from Medium

Are your algorithms biased?

Approximation Algorithms

Debug Your Python Code like a Master

Normalization: BCNF (Boyce-Codd Normal Form)