setTimeout(), clearTimeout(), setInterval() and clearInterval() : Everything you need to know

Let us understand these methods in a similar order as given in the title.

1. setTimeout() method:

This method allows us to run a function (or execute a piece of code) once the given interval of time expires.

Here's the syntax:

let timeoutID = setTimeout(functionName, time in milliseconds, arguments(if any))        

timeoutID is the value returned by the setTimeout. Although, we can name it anything.

Let's see an example:

function greet(){
     console.log("Welcome to my blog")
  }

setTimeout(greet, 5000)        
No alt text provided for this image

Output: It will print "Welcome to my blog". But it will take 5 seconds (5000 milliseconds) to do so (after the code is being run).

Let's see another example with a function that has parameters:

function greet(name){
     console.log("Welcome to my blog " + name)
  }

setTimeout(greet, 5000, "Elon")        


?[Click to continue reading...]

要查看或添加评论,请登录

Rajat Gupta的更多文章

  • pseudo classes in css part 1 (:hover)

    pseudo classes in css part 1 (:hover)

    Note: This is the first part of the series dedicated to the pseudo-classes of CSS. In this part, we'll understand the…

  • useContext in react: everything you need to know

    useContext in react: everything you need to know

    only understand this: Using useContext is not necessary, we use it to avoid prop drilling. Here's how prop drilling…

  • Sibling combinator in css

    Sibling combinator in css

    There may be some confusion as what we are calling sibling. So, let's first get that out of the way.

  • rest parameter in javascript

    rest parameter in javascript

    The rest parameter is introduced in ES6. It allows a function to accept an indefinite number of arguments as an array…

    1 条评论
  • PropTypes in react

    PropTypes in react

    Let's see what reactjs.org has to say: As your app grows, you can catch a lot of bugs with type-checking.

  • What the heck are props in react

    What the heck are props in react

    Although we can make web apps using JavaScript. One of the reasons we are using react over JS is component reusability.

  • JavaScript: single-threaded and synchronous

    JavaScript: single-threaded and synchronous

    A few days ago single threaded and synchronous were just 2 heavy words for me. If this is you right now, don't worry…

    1 条评论
  • reduce() method in JavaScript

    reduce() method in JavaScript

    Let's see what MDN has to say: The reduce() method executes a user-supplied “reducer” callback function on each element…

  • filter() method in JavaScript

    filter() method in JavaScript

    Definition by MDN: The filter() method creates a new array with all elements that pass the test implemented by the…

  • map() method in JavaScript

    map() method in JavaScript

    Let's see what MDN has to say: The map() method creates a new array populated with the results of calling a provided…

社区洞察

其他会员也浏览了