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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:18:06+00:00 2026-06-11T16:18:06+00:00

I have a module that has an internal loop (using ‘settimeout’). I want to

  • 0

I have a module that has an internal loop (using ‘settimeout’). I want to fire a callback each time the timer happens. I tried using jQuery’s deferred object with no luck.. Something like:

 $(function(){
    var module = new MyModule();
    $.when(module.init()).then("DO Something");
 });

 function MyModule(){

    this.d = $.Deferred();

}

test.prototype.init = function(){
    clearTimeout(this.next);

    var self = this;

    /*  Do Something */

    self.d.resolve();

    this.next = setTimeout(function(){
        self.init(); /* The problem is here - internal call to self */
    }, 4000);
    return self.d.promise();
};

The problem is that the timer calls the method internally, so I will not have a call to “.then(Do Something);” of the main program. I can use the old school function callback (pass a callback function to the module) but I really wanted to try these great feature.

Thanks,

Yaniv

  • 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-11T16:18:07+00:00Added an answer on June 11, 2026 at 4:18 pm

    A deferred really isn’t what you’re looking for because that’s a one time deal – what you likely want is a Callback list.

    jQuery provides that as $.Callbacks() which might be what you’re looking for.

    function MyModule(){
    
        this._initCallbacks = $.Callbacks();
    
    }
    
    MyModule.prototype.onInit = function( cb ) {
        if ( typeof cb === "function" ) {
            this._initCallbacks.add( cb );
        }
    };
    
    MyModule.prototype.init = function(){
        clearTimeout( this.next );
    
        var self = this;
    
        this._callbacks.fire();
    
        this.next = setTimeout(function(){
            self.init();
        }, 4000);
    
        return this;
    };
    
    $(function(){
        var module = new MyModule();
    
        module.onInit(function() {
            console.log( "Do something" );
        });
    
        module.init();
    });
    

    JSFiddle: http://jsfiddle.net/SUsyj/

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

Sidebar

Related Questions

Let's say that I have a module that has a Queue in it. For
Suppose you have a module that you know is safe. You want to mark
I have found DoctrineExtensions module that has implement some useful MySQL functions. But I
I have a module that has multiple class variables. I'm looking for a class
I have a view module that has the html and the javascript for uploading
I have an android project that has a test module that will remove and
I have a desktop app that has 65 modules, about half of which read
We have a Zend application that has these following modules: Users Shop etc... Front
I have a directory tmp that has 3 test node.js modules [mod0.js, mod1.js, mod2.js]
I have a Python library that, in addition to regular Python modules, has some

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.