Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:40:52+00:00 2026-06-03T09:40:52+00:00

Was wondering where I am going wrong with this snippet of code, basically the

  • 0

Was wondering where I am going wrong with this snippet of code, basically the startTime() only gets run once and thats it so the clock doesnt update. Can anyone advise how I can fix this?

JS

function startTime(){
  var today=new Date();
  var h=today.getHours();
  var m=today.getMinutes();
  var s=today.getSeconds();
  // add a zero in front of numbers<10
  m=checkTime(m);
  s=checkTime(s);
  document.getElementById('txt').innerHTML=h+":"+m+":"+s;
  t=setTimeout('startTime()',500);
  }

  function checkTime(i){
  if (i<10)
    {
    i="0" + i;
    }
  return i;
  }

HTML

    <body onload="startTime()">

<p>Date</p>
<?php echo date("d/m/y"); ?>

<p>Time</p>
<div id="txt"></div>

Link http://bit.ly/IC9ohX

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:40:54+00:00Added an answer on June 3, 2026 at 9:40 am

    You just have a problem with the scoping of your functions.

    Short solution: Change

    t=setTimeout( 'startTime()',500);
    

    to this.

    t = setTimeout( startTime, 500 );
    

    Explanation:

    The function startTime is defined within the function for window.onload and is only visible within that function (and functions within it)!

    In your code JavaScript waits the 500ms and then tries to execute the code startTime(). It searches for the function startTime in the global scope (which is the execution scope of setTimeout) and can’t find any reference, thus resulting in an error and your code not working.

    My code passes the reference of the function startTime to setTimeout. This way the engine does not have to search for the handler and can’t fail at that point. At the point where setTimeout is called, you are in the scope of window.unload, thus the engine knows of the function startTime and can cope with the handler passed to setTimeout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am probably going about this in the wrong manner, but I was wondering
I'm most likely going about this the wrong way, but I was wondering if
Right, this is a very odd issue that I'm wondering how i am going
Ok, i'm wondering if this code stored in user_login.php could be improved or if
This is going to sound like a dumb question, but I was wondering where
I was going through the RMI documents and was wondering if it is possible
I'm going to implement an web app in gae. I was wondering which is
Wondering if I could get some advice and direction on this following requirement: Need
I was wondering if someone could work this one out as I've been trying
Started using thinking sphinx today and I'd like to know what's going wrong here:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.