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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:14:16+00:00 2026-06-08T01:14:16+00:00

I was wondering if someone could give me a simple solution to this. I

  • 0

I was wondering if someone could give me a simple solution to this.

I want to have the two dog instances repeat the bark + wag tail combination.

If all logic is contained within the barkNameAndTime function, I’m fine, but by calling wagTail via the setTimout I lose context. I don’t write much JS so my closure knowledge is a litle rust and I can’t quite seem to find a clean solutions.

Problem is that Baloo ends up doing all the barking and tail wagging and Lola is lost in the crowd 🙂

Thanks
Warrick

<script type="text/javascript">
$(document).ready(function () {
    var zDog1 = new dog("Lola");
    var zDog2 = new dog("Baloo");

    zDog1.bark();
    zDog2.bark();
});

function dog(aName) {
    var name = aName,
        barkNameAndTime = function () {
            var time = new Date().getTime();
            $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + " barked @ " + time);
            setTimeout(wagTail, 1000);
        };

        wagTail = function () {
            var time = new Date().getTime();
            $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + "  wagged tail @ " + time);
            setTimeout(barkNameAndTime, 1000);
        }

    return {
        bark: barkNameAndTime
    };
}

  • 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-08T01:14:18+00:00Added an answer on June 8, 2026 at 1:14 am

    In your implementation, wagTail is a global variable (probably by mistake) so its one global value and context gets replaced each time you create a new dog object.

    If you make it a local variable (by changing one semicolon to a comma), it should work.

    function dog(aName) {
        var name = aName,
            barkNameAndTime = function () {
                var time = new Date().getTime();
                $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + " barked @ " + time);
                setTimeout(wagTail, 1000);
            }, 
    
            wagTail = function () {
                var time = new Date().getTime();
                $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + "  wagged tail @ " + time);
                setTimeout(barkNameAndTime, 1000);
            }
    
        return {
            bark: barkNameAndTime
        };
    }
    

    When it’s a local variable, then each copy of wagTail keeps its own closure context (and thus access to the local variables in its same stack frame). When it’s a global variable, all calls to wagTail will have the same context which will be the context the last time it was assigned to.

    FYI, this is one of the reasons I don’t like this way of declaring local variables when there are multiline initializers as one subtle typo renders some of them implicit global variables. I’d rather predeclare the locals and then assign to them in the body. Much less likely to suffer this error. Or, in this case, you don’t need to use the variable syntax, you could have just declared barkNameAndTime and wagTail as local functions (code example added by Bergi):

    function dog(name) {
        function barkNameAndTime() {
            var time = new Date().getTime();
            $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + " barked @ " + time);
            setTimeout(wagTail, 1000);
        }
        function wagTail() {
            var time = new Date().getTime();
            $('#MyDiv').html($('#MyDiv').html() + "<br />" + name + "  wagged tail @ " + time);
            setTimeout(barkNameAndTime, 1000);
        }
    
        return {
            bark: barkNameAndTime
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Was wondering if someone could give me some direction on this. I've spent a
Hi i was wondering if someone could give me a hand with this... I've
I've started using RhinoAutoMocker and I was wondering if someone could give me hand
I don't really understand Perl, so I was wondering if someone could give me
Wondering if someone could please explain the difference between these two queries and advise
Wondering if someone could help me. I have next to no knowledge with Ajax,
Just wondering if someone could help me with a very simple SQL query. I
I was wondering if someone could explain to me how to make a simple
was wondering if someone could help with this one. This is the first time
my java is rusty and I was wondering if someone could give me a

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.