Countdown var to loop and display array
I'm very new to javascript and I'm trying to do something I thought would be very basic.
I've created a countdown timer and used "i" as my variable to hold a number from 0-5. And I have an array of "d" from d[0] to d[5] containing strings.
I'm trying to make the timer countdown pass the "i" value into an innerHTML method array value so I want it to display d[5]... d[4]...开发者_高级运维d[3]... etc.
What am I doing wrong!? Please Help!
<html><head><script language="javascript" type="text/javascript">
var d=new Array():
d[1]="One";
d[2]="Two";
d[3]="Three";
d[4]="Four";
d[5]="Five";
var i=5;
var i=setInterval("timer()",2000); //1000 will run it every 1 second
function timer() {
i--;
if (i <= 0)
clearInterval(countD);
return;
}
}
document.getElementById(timer).innerHTML = d[i];
</script>
</head>
<body>
<h1>
<p id="timer"></p>
</h1>
</body>
</html>
Also, document.getElementById(timer).innerHTML = d[i];
should be document.getElementById("timer").innerHTML = d[i];
id names need to have quotes around them because they are not names of variables. The variable 'timer' is undefined.
Also, you are missing a curly brace on the line if (i <= 0)
. I am assuming that you meant to exit the function if this if statement is true.
Also, you have a colon instead of a semicolon on the line var d=new Array():
Also, you can't have a paragraph tag inside of an h1
Also, you should encapsulate all of this javascript into a function called something like init
. I believe that the javascript code in the head runs before the html is loaded. The javascript can't therefore find the
tag. Then, use <body onload="init()">
as your body tag.
EDIT: As the commenters have stated, you are using the variable i for multiple unrelated things.
I'm sorry to say but your code is quite a mess; here's one way to get your code to work, along with a working example:
<html>
<head>
<script language="javascript" type="text/javascript">
var d=new Array();
d[0]="One";
d[1]="Two";
d[2]="Three";
d[3]="Four";
d[4]="Five";
var i=4;
var myTimer =setInterval(timer,2000); //1000 will run it every 1 second
function timer() {
document.getElementById("timer").innerHTML = d[i];
i--;
if (i < 0){
clearInterval(myTimer);
}
}
</script>
</head>
<body>
<h1>
<p id="timer"></p>
</h1>
</body>
</html>
The problems with your JavaScript
- You've got two declarations for
i
, one after the other - one is set to5
and the other to thesetInterval
timer - What is
countD
inclearInterval(countD);
? document.getElementById(timer).innerHTML = d[i];
is attempting to use the functiontimer
as the argument togetElementById
. It should be in quotes:document.getElementById("timer").innerHTML = d[i];
- It is also being invoked only once since it's not inside the function that is being called by the timer.
var d=new Array():
should be terminated by a;
(semi-colon) and not a:
(colon)- You have unmatched braces - your
if
statement doesn't have an opening brace ({
) but it does have a closing one. - Note that while
setInterval("timer()", 1000)
is valid JavaScript, it depends oneval
which should be avoided if possible. The alternate, preferred way to use this issetInterval(timer, 1000)
i.e. passing the function and not a string.
精彩评论