You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • You'll need to use either a global variable, or a closure.

    In your example, simply doing:

    function texte(){
        var idAnim = setInterval(function(a,y,c){
          console.log(a,y);
          compteurAnim++;
          if (compteurAnim >= a.length) { clearInterval(idAnim);}
        }, 100, mot, 0);
        mot+="foo";
    }
    

    will work. idAnim will then be defined in the function each time it is called, and the setInterval callback will be able to reference it.

    As I think you figured out, in idAnim = setInterval( anim, 500, "foo", 0, idAnim); the setInterval has to be called before idAnim is set to the correct return value, so you'll always get the wrong answer in there.

About

Avatar for Gordon @Gordon started