+ 1
Why am i getting error?
Help me fix this. https://code.sololearn.com/W5oNIU3S9pRz/?ref=app
6 Answers
+ 3
change the onload to this
window.onload=main;
then the set interval to
setInterval(()=>move(box),500)
you also have typo for id button
+ 1
Not working, the event handler starts on the go, doesn't waits.
+ 1
not recomended way to write it, but here
startBttn.onclick=e=>setInterval(()=>{move(box)},500);
you can clean it up
+ 1
Why can't i use main() instead of main & move(box) instead of move?
+ 1
on-something require a function to work, when you use main() it will run the function and take its returned value instead of the function itself
so in your previous code
window.onload = main();
main will executed and the value it returned (which is undefined) will be assigned to onload, its all because you're using ().
but without (), the browser wont execute the code, and instead treat the function itself like normal value to passed around
+ 1
Ok thankyou very much.