From the course: Learning Functional Programming with JavaScript (ES5)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Simple callbacks

Simple callbacks - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

Simple callbacks

- [Instructor] The simplest example of using a callback in JavaScript is the built in function setTimeout. The purpose of this function is simply to delay the execution of another function for a certain number of milliseconds. In practice this function is used mainly for user interface tweaks, such as having pop-ups appear after several seconds or cycling through slideshows. Let's start with a simple example to show you how this function works. All we have to do is pass setTimeout a function and a duration. Remember that this duration is expressed in milliseconds. Inside this function we can do whatever we want. For now, we'll just print to the console. And for sake of demonstration, we're going to log something to the console before we call setTimeout. Let's run our code to see what happens. Now as we can see, the function that we passed to setTimeout isn't executed until after 3,000 milliseconds, or three seconds. Now what happens if we execute a statement After setTimeout? Now you…

Contents