0
help me!
why "start()" function is undefined? https://code.sololearn.com/WBH4l5WmXg7x/?ref=app
3 Respuestas
+ 4
sherlockholmes ,
not only start() but all other functions and variables that you have defined within window.onload will give you the same error if you try to access them from outside of window.onload
Functions and variables defined inside a function scope are accessible only inside that function.
to remove error move all function definitions and variable declarations outside of window.onload method to make them accessible globally , as follows :
var right = 0;
var left = 150;
var box;
function start(){
var t = setInterval(move, 10);
}
function move() {
if(left == 0){
left = 150;
right = 0;
}
if(right == 150) {
left -= 1;
box.style.left = left+'px';
}
if(right < 150){
right += 1;
box.style.left = right+'px';
}
}
window.onload = function() {
box = document.getElementById('box');
};
+ 1
its work! thanks
0