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.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
You'll need to use either a global variable, or a closure.
In your example, simply doing:
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);
thesetInterval
has to be called beforeidAnim
is set to the correct return value, so you'll always get the wrong answer in there.