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

  • Home
  • SEARCH
  • 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 7873661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:34:48+00:00 2026-06-03T02:34:48+00:00

I started trying to create a timer function that would let me wrap a

  • 0

I started trying to create a timer function that would let me wrap a callback function so that I could later alter the behavior dynamically.

This led to a general realization that I really don’t understand functions yet, and definitely don’t understand what is happening with ‘this’

I have a test environment setup on jsfiddle

myns = {};
myns.somefunc = function(txt) {
    this.data = txt;
    this.play = function() {
        alert(this.data + ' : '+dafunc.data);
    };
};

var dafunc = new myns.somefunc('hello world');

myns.Timer = function(msec, callback) {
    this.callback = null;
    this.timerID = null;

    this.ding = function() {
        this.callback();
    };

    this.set1 = function( msec, callback ) {
        this.stop();
        this.callback = callback;
        this.timerID = setTimeout(this.ding, msec );
    };

    this.set2 = function( msec, callback ) {
        this.callback = callback;
        var wrappedDing = (function(who) {
            return function() {
                who.ding();
            };
        })(this);
        this.timerID = setTimeout(wrappedDing, msec );
    };
    //this.set1(msec, callback);
    this.set2(msec, callback);    
};

var ttimer = new myns.Timer(1000, dafunc.play);

If I use the set1 method, then the callback doesn’t work.
So I am trying the set2 method. This gets me to the play method but “this” is not referring to the instance of somefunc.

I thought I was on the right track, but the mix up on ‘this’ has me confused.

Any clues would be welcome.

  • 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-03T02:34:50+00:00Added an answer on June 3, 2026 at 2:34 am

    The problem is that, unlike in a language like python, when you take a dafunc.play and pass it somewhere else (callback = dafunc.play) it forgets it was associated with dafunc, son you you would need to use yet another wrapper function, like you did in the set2 function.

    var ttimer = new myns.Timer(1000, function(){ return dafunc.play(); });
    

    Making all there extra functions by yourself is annoying. You could instead use the bind method that is available in newer browsers:

    var wrappedDing = this.ding.bind(this);
    
    new myns.Timer(1000, dafunc.play.bind(dafunc) );
    

    Or you could use a similar shim if you need to support older versions of IE too.


    Finally, if you are not going to take advantage of some form of inheritance or dynamic binding, you could instead rewrite your code to use closures. Since everything is lexicaly scoped, you don’t have to worry about the this anymore:

    (btw, I ended up simplifying the code in the proccess…)

    myns = {};
    
    myns.somefunc = function(txt) {
        var obj = { data : txt };
        obj.play = function() {
            alert(obj.data);
        };
        return obj;
    };
    
    var dafunc = myns.somefunc('hello world');
    
    myns.timer = function(msec, callback) {
        var timerID = null;
    
        var set = function(){
            stop();
            timerID = setTimeout(callback, msec);
        };
    
        set();
    
        return { 
             set: set
        };
    };
    
    var ttimer = myns.timer(1000, dafunc.play);
    

    And one last thing: If you don’t hate yourself use console.log and your browser’s debugger and development console instead of using alerts for output.

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

Sidebar

Related Questions

I am trying to create a function which adds a text box (a blue
I'm trying to create a Tkinter GUI that saves all entry box values into
I'm trying to create a Redirect timer plugin from all JS (JQuery) but for
I am trying to create a generic Identifier class which I would be able
Im trying to create a small game in C and SDL to get started
I'm trying to create a simple program that does the following: A service (NewsService)
this is my first post on this website, Anyway, I recently started trying to
I started by trying to store strings in sqlite using python, and got the
I'm just digging a bit into Haskell and I started by trying to compute
I'm having real problems getting PDO_MYSQL working. I started by just trying to install

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.