Skip to content

Instantly share code, notes, and snippets.

@rnaffer
Last active October 27, 2015 18:57
Show Gist options
  • Save rnaffer/fcbf7c5d6aae50981e26 to your computer and use it in GitHub Desktop.
Save rnaffer/fcbf7c5d6aae50981e26 to your computer and use it in GitHub Desktop.
Ejemplo del patrón de diseño "Recursive SetTimeout" con explicación.
// El orden empleando setInterval() puede ser impredecible ( 0 1 2 5 4 6 8 7).
// Invocar recursivamente a setTimeout() puede asegurar el orden de ejecución ( 0 1 2 3 4 5 6).
// Hasta que no finalize una ejecución no continua con las demás.
var ul = $('ul.log'),
index = 0;
setTimeout(function getDate() {
var started = new Date(),
i = index;
index++;
$.get( '/home/date', function( date ) {
var end = new Date();
ul.append( '<li>Request ' + i + ' started at ' + started.getHours() +
'. Finished: ' + end.getHours() );
// LLamada recursiva
setTimeout( getDate, 5000 );
});
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment