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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:11:31+00:00 2026-06-02T06:11:31+00:00

after reading JavaScript: The Good Parts of Douglas Crockford, I tried to implement a

  • 0

after reading “JavaScript: The Good Parts” of Douglas Crockford, I tried to implement a timer like this. It has private variable (seconds, hours, minutes) and three public methods: start, stop and continue:

var timer = function() {
            var that = {};
            var seconds = 0;
            var hours = 0;
            var minutes = 0;
            var myTimer;

            that.getTime = function() {
                var time = hours + " : " + minutes + " : " + seconds;
                return time;
            }
            that.start = function() {
                seconds += 1;           
                if(seconds >= 60) {
                    seconds -= 60;
                    minutes += 1;
                }
                if(minutes == 60)
                    hours += 1;
                document.getElementById('hours').innerHTML = hours;
                document.getElementById('minutes').innerHTML = minutes;
                document.getElementById('seconds').innerHTML = seconds; 
                myTimer = setTimeout(function() {
                    start();
                }, 1000);
            };

            that.stop = function() {
                clearTimeout(myTimer);
            }
            that.reset = function() {
                seconds = 0;
                hours = 0;
                minutes = 0;
                clearTimeout(myTimer);
                document.getElementById('hours').innerHTML = hours;
                document.getElementById('minutes').innerHTML = minutes;
                document.getElementById('seconds').innerHTML = seconds;
            }
            return that;
        };

and then, I started it:

<body onload="var t = timer();t.start();">

    <h1>Digital Clock</h1>
    <div id="wrap">
        <div>
            <ul>
                <li id="hours"></li>
                <li> : </li>
                <li id="minutes"></li>
                <li> : </li>
                <li id="seconds"></li>
            </ul>
        </div>
    </div>
    <br/>
</body>

Can anyone tell me what errors did I make ?

Update: Finally, I found the problem.When you use a function from within another function (say, inner function), “this” is bound to global, not the outer function. Thus, in the statement start(), js will try to find a function in global object. Of course, there is no such function like this. Here, I found two solution:

  1. use “that”

    myTimer = setTimeout(function() {
    that.start();
    }, 1000);

  2. Save the context:

    var timerInstance = this;

    myTimer = setTimeout(function() {
    timerInstance.start();
    }, 1000);

Hope this will help you.

  • 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-02T06:11:32+00:00Added an answer on June 2, 2026 at 6:11 am

    Here is a jsFiddle containing your modified code: http://jsfiddle.net/6YWLJ/

    I’ve replaced “that” with “this” (“this” represents your timer) and instead of “var timer” i’ve used “window.timer”

    Later edit: this is a version of corrected code, so it acts like a timer (you had some bugs in your code) http://jsfiddle.net/6YWLJ/2/

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

Sidebar

Related Questions

After reading Douglas Crockford's JavaScript: The Good Parts and Stoyan Stevanov's JavaScript Patterns, I'm
I'm reading crockford's Javascript: The Good Parts and am messing around with this piece
I am trying to learn JavaScript. After reading this page: What does ':' (colon)
Reading 'Javascript The good Parts' from Crocksford, I've been in a quest to write
I started with this question: Multiple Javascript gadgets per page . After reading up
After reading this blog post: http://www.sitepoint.com/javascript-shared-web-workers-html5/ I don't get it. What's the difference between
After reading this article net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/ I came to conclusion that using this.href is more
I've been playing around with prototypal inheritance after reading http://javascript.crockford.com/prototypal.html and having a bit
EDIT I'd still like to know why this happened but after reading some stuff
I started reading JavaScript: The Good Parts book and became confused at first pages

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.