Created
April 11, 2011 13:40
-
-
Save ggendre/913527 to your computer and use it in GitHub Desktop.
javascript : smart setTimeout
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
function delayed(){ | |
alert('toto'); | |
} | |
//affiche toto dans 5 secondes | |
setTimeout(delayed,5000); | |
//affiche titi dans 7 secondes | |
setTimeout(function(){ | |
alert('titi') | |
},7000); | |
//attention: | |
//la fonction appelée hérite des variables locales | |
//sauf 'this'. | |
//sauvegardez le dans une variable locale si vous en avez besoin : | |
var toto="toto" | |
var _this=this; | |
setTimeout(function(){ | |
console.log(toto); // affichera "toto" | |
console.log(this); // ne marchera pas, this sera null ou undefined | |
console.log(_this); //_this sera égal au this d'avant. vous pouvez l'utiliser | |
},7000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment