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 9002593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:35:29+00:00 2026-06-16T00:35:29+00:00

i got a task that ask me to create a page, which can display

  • 0

i got a task that ask me to create a page, which can display a clock after

the user click on the start button and stop when click the stop button

The clock need to display inside a “span” tag because i need the DOM1 method.

So my Code currently look’s like this

*I had already fix this.

function clock()
{


var obj = document.getElementById('clock');
if (obj)
{
var now = new Date();
var mytime = now.getHours()+":"+now.getMinutes()+"."+now.getSeconds();
obj.innerHTML = mytime;

}
}




</script>

</head>

<body onload="clock()">
<table>
<h1>A live clock in JavaScript</h1>

<p>The time according to your PC is :<span id="clock"></span></p>




<input type="button" name="clickMe" value="Start the clock"
onclick="timer = setInterval(clock, 1000)"/>


<button onclick="window.clearInterval(timer)">Stop</button>
</table>
</body>
</html>
  • 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-16T00:35:30+00:00Added an answer on June 16, 2026 at 12:35 am
    obj.document.getElementById('clock');
    

    Since obj is undefined at this point, it will throw a referenceError. Perhaps you meant:

    var obj = document.getElementById('clock');
    

    if (obj != "false")
    

    This will most likely not work, since you’re comparing to a string. If you want to check whether obj is an object or null, you can use

    if (obj)
    

    obj = mytime;
    

    this will not modify the original obj, but rather overwrite the variable holding a reference to it. Perhaps you wanted

    obj.innerHtml = mytime;
    

    also change the HTML from

    <p id="clock">The time according to your PC is :</p>
    

    to

    <p>The time according to your PC is :<span id="clock"></span></p>
    

    so that you don’t overwrite the text but do overwrite the clock.


    onclick="setTimeout('"
    ...
    onclick="int=window.clearInterval(int)"
    

    there is a part missing:

    onclick="timer = setInterval(clock, 1000)"
    ...
    onclick="window.clearInterval(timer)"
    

    where the first argument is the function being called repeatedly, and the second argument is the frequency of the calls in miliseconds. Also note that the return value of clearInterval is not particularly useful since the argument to it should be the value returned by setInterval.

    Also note the distinction between setTimeout (which will call a function once) and setInterval (which will call it repeatedly).

    Also note that the timer variable now resides in the global scope. This could be a problem in larger projects. You also have a problem if you start the timer several times as you can’t stop it now. Consider this instead (at the end of body) (and assign the correct IDs to both inputs):

    (function(){ //create a scope
      var timer; //create a local variable
    
      document.getElementById("clock-start")
      .addEventListener("click", function(){
        if(!timer) timer = setInterval(clock, 1000);
      })
    
      document.getElementById("clock-stop")
      .addEventListener("click", function(){
        clearInterval(clock);
      })
    })() // remember to call our anonymous function
    

    or the jQuery version (anywhere in the document):

    $(document).ready(function(){ //execute on DOM ready
      var timer;
    
      $("#clock-start").on("click", function(){
        if(!timer) timer = setInterval(clock, 1000);
      })
    
      $("#clock-stop").on("click", function(){
        clearInterval(clock);
      })
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive got task to create application which can post data on users friends walls.
So I've got a task that can be preformed by my GUI that will
I've got a common task that I do with some Activities - downloading data
I've got an Ant task that when launched will update the version number in
I've got a build.gradle task that works like a champ on my dev box
I've got task to do and I even dont know how to start. I
I got handed the task to create a Windows Phone app and i am
If got a task to create a mobile application for iOS, Android and WP.
I'd like to ask about designing workflow merging. I've got several workflows that aren't
I got a task that I am not sure how best to do it.

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.