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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:51:35+00:00 2026-05-25T21:51:35+00:00

I am taking an object-oriented approach to building a Javascript app. For starters, I

  • 0

I am taking an object-oriented approach to building a Javascript app.
For starters, I am creating a Game object and a Timer object.

Here’s my current implementation:
The Timer object has the following methods:

setTimerValue()  
start()  
stop()  
getTimeLeft()  
timeExpired()

The Game object has the following methods:

reset()
run()
...

Timer.start() uses window.setInterval() to start the countdown.
When the countdown gets to zero, it calls Timer.timeExpired().

Timer.timeExpired() announces that the game is over and should call Game.reset() and Game.run() to restart the game. But how can this be done when the Timer object doesn’t know anything about the Game object?

Does the Game object need to be a global variable for this to work?

Also, the Game object currently has a Timer object as one of its properties. This allows the Game object to call the Timer methods. But unclear on how Timer methods can call the Game object.

I have a nagging suspicion that I am creating too many dependencies between the objects and could use some suggestions on how to architect this better.

Thanks everyone.

  • 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-05-25T21:51:36+00:00Added an answer on May 25, 2026 at 9:51 pm

    Use an event driven approach or the observer design pattern. Javascript has one of the flexible event driven implementation from my experience.
    So you have the Timer object which doesn’t really have to know about any other objects.
    When you call Timer.start(), the object should emit a custom event, like “timerStarted”, likewise for the other methods:

    Timer.stop() - > emits "timerStoped"
    Timer.timeExpired() -> emits "timerExpired"
    

    For every event raised by the Timer, there should be an event handler, which will be called when the coresponding event is fired.

    If you use jquery, handling events should be trivial:

    var $ = jQuery.noConflict();
    var timer = new Timer();
    var game  = new Game();
    
    $(timer).bind('timerStarted', function(e) {
        game.doSomething();
    });
    

    in the Timer::start() you should write:

    start: function() {
        $(this).trigger('timerStarted');
    }
    

    Another approach is using callbacks:
    If you want something to be done when the timer starts, you could pass a lambda function to the start method of the timer:

    // timer method
    start: function(callback) {
        if(typeof(callback) == 'function') {
            callback();
        }
    }
    
    // main logic
    var timer = new Timer();
    var game  = new Game();
    
    timer.start(function() {
        game.doSomething();
    });
    

    I hope you get the ideea

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

Sidebar

Related Questions

I am new to taking an object oriented approach with php...actually I have a
Recently, while taking an introductory unit focused on Object Oriented programming, I was introduced
A co-worker of mine is taking an Introduction to Object Oriented Programming class and
I'm working through this tutorial: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php At first he has you create a setter
I read this book: Head First Object-Oriented Analysis and Design Now i am looking
I have a bitmap object which is taking a huge part of the memory
So I am a little confused on the object oriented part of PHP. Right
I'm working on a project in C#. The previous programmer didn't know object oriented
This past semester I took intro to object oriented programming in java and next
Taking the following object model: public abstract class Entity { public Guid Id {

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.