For Loop Not working with inner html java script

Multi tool use


For Loop Not working with inner html java script
I am writing a code in java script to display a countdown from 10 to 0. Loop is working for alert but when i use document.getElementById the loop doesn't work and it shows 0. Here's My Code
heading is the id of h1
function msg(z)
{
try
{
for(var i=0;i<=10;i++)
{
var seconds=z-i;
document.getElementById("heading").innerHTML="Select T
The Multiples of 2 in"+" "+seconds+" "+"seconds";
}
}
catch(e)
{
document.write(e);
}
setTimeout
setInterval
1 Answer
1
Have you use debug tools to verify if the problem is in the loop? You are not waiting any second in each loop iteration, so youll see the end result as it will execute in much less than a second, so youll see 0 (the final iteration)
Take a look at this post Javascript Second Counter as is basically the same as you are trying to do.
Hope this helps
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
There's no delay, you're going through the loop overwriting the previous value so fast you can't see the output. Look at
setTimeout
orsetInterval
– DBS
1 min ago