window.onload = function(){ function translationMap(a,b){ ctx.translate(a,b); } function majMap(sens){ var cpt; switch(sens){ case 1: for(cpt=0;cpt<16;cpt++){ var horloge = setInterval('translationMap(0,2);' , 100); } clearInterval(horloge); break; case 2: for(cpt=0;cpt<16;cpt++){ var horloge = setInterval('translationMap(0,-2);' , 100); } clearInterval(horloge); break; case 3: for(cpt=0;cpt<16;cpt++){ var horloge = setInterval('translationMap(-2,0);' , 100); } clearInterval(horloge); break; case 4: for(cpt=0;cpt<16;cpt++){ var horloge = setInterval("translationMap(2,0);" , 100); } clearInterval(horloge); break; } } }
But when I'm executing majMap(); it told me :
"Uncaught ReferenceError: translationMap is not defined";
What can be wrong and how do I fix that?
You are passing a string to setInterval
which would require all referenced code to be in the global scope ( window
). translationMap
only exists in the scope of window.load
. Move translationMap
outside of this method.
function translationMap() {}
window.onload = function(){
}
Furthermore, I would recommend not using a string for setInterval
, but instead, actually pass a function:
setInterval(function () { translationMap() }, 100);
This should also work and is a better solution than requiring javascript to use an eval
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.