Last active
October 27, 2015 18:57
-
-
Save rnaffer/fcbf7c5d6aae50981e26 to your computer and use it in GitHub Desktop.
Ejemplo del patrón de diseño "Recursive SetTimeout" con explicación.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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