Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript - Set Interval Problem

Featured Replies

Hey,

 

I have a problem, I have my webpage all set out, and I want the time to update every second.

 

I have my function updateTime() in which is displays the time.

 

Then i want this to be updated every second via the setInterval function.

 

So this is what I have:

 

			function updateTime() {
			var now = new Date();
			var time = 'The current time is: ' + ( now.getHours() % 12 ) + ':' + pad( now.getMinutes() ) + ':' + pad( now.getSeconds() ) + ' ' + ( now.getHours < 12 ? 'AM' : 'PM' );
			document.getElementById("time").innerHTML = time;

		}

 

Then, my window onload

 

		window.onload() {
		updateTime(); // So the page doesnt have to wait a second for the time to load
		setInterval( "updateTime()", 1000 );
		}

 

Now, the time loads (through the updateTime(); but the time doesnt refresh every second! I really cant work it out, anyone notice anything?

You're sending the setInterval a string, not a function. To get around that, take out the quotation marks and brackets:

window.onload() {
                       updateTime(); // So the page doesnt have to wait a second for the time to load
                       setInterval( updateTime, 1000 );
                       }

 

That should fix it

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.