From the course: JavaScript for Web Designers

Unlock the full course today

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

Use timers to update the page

Use timers to update the page - JavaScript Tutorial

From the course: JavaScript for Web Designers

Start my 1-month free trial

Use timers to update the page

- [Man] At this point we have a clock that will be current when the page first loads. But what I'd really like is for the clock to stay current as long as I have the page open. That means I need some way to update it every, say, minute or so. In JavaScript we use timers for this purpose. Let's see how they work. We have available a couple of different timer functions in JavaScript. One of them is called setTimeout, the other is called setInterval. If you've looked at other people's JavaScript code before, you may have seen one or the other of these. We'll start with setTimeout. SetTimeout is a function that takes two arguments. The first one is a function that we want to execute after a certain delay. And the second parameter is how long that delay should be. So let's create a function called updateTime. And we'll wrap all this code to actually perform that update. So I'll cut all this and put it inside…

Contents