0

help me!

why "start()" function is undefined? https://code.sololearn.com/WBH4l5WmXg7x/?ref=app

9th Jan 2020, 2:21 PM
sherlockholmes
sherlockholmes - avatar
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'); };
9th Jan 2020, 2:39 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
its work! thanks
9th Jan 2020, 2:51 PM
sherlockholmes
sherlockholmes - avatar
9th Jan 2020, 2:54 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar