Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
what's wrong with this code?
I think it's perfect but never work... :( Please help! countdown(3); function countdown(num) { var c = setInterval(function() { document.write(num); if (num == 0) { clearInterval(c); } num--; }, 1000); }
2 Answers
+ 4
Works for me. If it persists in-app, you can try updating Google WebView or SoloLearn.
The HTML5 test suite (not mine):
https://code.sololearn.com/WeBKGPjY0UHK/?ref=app
can reveal if Chromium/WebView are old ("You are using Chromium WebView [version] on...")
+ 1
function countdown(num) {
var c = setInterval(function() {
document.write(num); // Maybe instead of this get the element you what to output to
// or create a new element and child text node or write to its innerHtml
if (num === 0) { // Should use === instead
clearInterval(c);
}
num--;
}, 1000);
}// you could also just call the function immediately by removing the call and adding )(3); here and ( before the f in function above, wrapping the whole function definition in parentheses.
countdown(3); // Usually better to call after it has been defined
Your code works just fine for me once the page loads (it may be where you have your script tags hint: place them at the bottom of the body so that the document and its elements actually exist when the script is ran and/or make sure that your code is only ran after the page loads see: http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load), but I made some minor suggestions above in comments.