+ 1
Why this error coming in Sololearn output tab?
my below web code showing error only in sololearn output tab. It is working in other browsers https://code.sololearn.com/WLfD2n8pN7D0/?ref=app
7 ответов
+ 4
Another option is to use setInterval() rather than setTimeout(), which will rather call the function in argument at interval specified ;)
+ 3
You try to access an Html element after 1000ms, so depending on browser/device, the document could be ready or not (when your JS is executed, the main content of the document isn't already parsed: if it take less than 1s you will not have error, else you will have: better to call your first function in the onload event of window object:
window.onload = init; // don't put parenthesis, as you assign the function itself, not calling it: it will be called when the event will be fired ^^
+ 3
Crashed again while testing...so I lost my edits, but here's the gist:
When using setTimeout, don't include the () on the function you're calling. If you do, setTimeout runs the result of the function after running the function.
You're also calling it in a while loop (like a setInterval() ...); a better strategy is to setTimeout() at the end of the function that is currently running, e.g.:
// no while necessary here -- this is the first start
function init() {
setTimeout(runMe, 1000);
}
function runMe() {
// do stuff
setTimeout(runMe, 1000);
}
The way you're calling it is actually leading to the Javascript engine getting stuck and resetting my app :/
You have an additional issue with referencing .style..., but I think you can get through it now.
+ 3
You also fixed the ()'s so it doesn't evaluate immediately / before the DOM's ready :)
The error you have now is the next one...so, a hint: style.color is not a function. It's like a variable (assign to it).
(style.color and style['color'] work the same way here, if it helps you analyze this)
+ 2
** Note, I lost this answer once due to a crash...I'll address that in a minute.
Because the [JS] tab runs in <head> before the DOM is loaded.
You can do either of these:
1: <body> ...... <script>init()</script></body>
2: [JS] setTimeout(init, 250); // line 21
Either of these will start init() after the div's have been inserted into the DOM. The second one isn't preferred because you shouldn't depend on a timer, but copy-paste is quicker.
+ 1
hi all thank you for your answers actually the error I am getting in my phone is unknown property style. I removed the while loop. browsers will not crash.
+ 1
thanks to all. finally it is working. check it out.@kirk special thanks to you.
https://code.sololearn.com/WLfD2n8pN7D0/?ref=app