I continue my crusade to the world of JavaScript. Faced the following difficulty:

function stub(callback){ var delay = Math.floor(Math.random()*10000)+1000; var msg = "Message " + delay; setTimeout(stub(callback), delay); callback(msg); } stub( function( msg ){ alert(msg); }); 

I execute in firefox and firebug shows an error:

 too much recursion: setTimeout(stub(callback), delay); 

How can this be circumvented? Like a recursion and does not smell, just an infinite loop :-)

UPD. Another jamb noticed the delay began to vary from 10,000 to 30,000 milliseconds. but something I do not feel this delay, what could be the problem?)

    3 answers 3

    First, you use setTimeout incorrectly. You pass not the function as the first parameter, but the result of its execution. This leads to the fact that there is no delay (in case you swapped). And if everything is as it is written for you, then you call the stub from the stub .. and that’s what turns out to be a recursion. (Call the stub instead of referring to the stub for timed out).

    UPD: To defeat it, you need to do this:

     function stub(callback){ var delay = Math.floor(Math.random()*10000)+1000; var msg = "Message " + delay; setTimeout( function() { stub(callback); }, delay); callback(msg); } stub( function( msg ) { alert(msg); }); 

    UPD2: "everything worked" - this is not true. The alert is just shown and you close it, re-enter the recursion again and the alert again .. just does not have time to overflow the stack :)

    • Tooting. - stanislav
    • One more function a wrapper to add? :-D - psyhitus
    • alert () is so for an example :-) - psyhitus

    And what is the meaning of the function and its execution on a timer?

    Just wondering how js works?

    I would solve the current problem like this:

     var i; function stub(__callback) { i++; var msg = 'Message ' + i; eval(__callback); }; setInterval(function () { stub('any code place here'); }, 1000); 

    If you are interested in js, I advise you to initially use prototypes, like:

     var Example = function(options) { this.sample = options['some_option']; this.__construct(); }; Example.prototype = { sample: null, __construct: function() { alert(this.sample); } }; var test = new Example({some_option: 'test value'}); 

    Functions are a good thing, but when you need to create a bunch of prototypes of an object, you can't invent anything better than prototypes, this is a structure that has its own methods and details. I advise you to study :)

    • Thanks, but I haven’t yet understood much about js) I’ve been studying the book by John Reisig - "JavaScript - Professional Programming Techniques" has not yet reached the prototypes :-) - psyhitus

    Changed the lines setTimeout (stub (callback), delay); c callback (msg); and it worked. Maybe someone has tips on how to do it better?) Who cares about the correct version, I hope :-)

     function stub(callback){ var delay = Math.floor(Math.random()*10000)+1000; var msg = "Message " + delay; callback(msg); setTimeout(function(){stub(callback)} , delay); } stub( function( msg ){ alert(msg); });