admin管理员组文章数量:1287146
I want to make a simple JavaScript countdown program. For some reason the countdown triggers without me having to click the start link.
<!DOCTYPE HTML>
<html>
<head>
<title id = "titleElement">My Top Ten Movies</title>
</head>
<body>
<p>Countdown Suren's favorite ten movies
<a href="" id = "start"> Start </a>
</p>
<table>
<thead>
<tr>
<td>Number</td>
<td>Movie</td>
</tr>
</thead>
<tbody id = "movieList">
</tbody>
</table>
<script src="js/practice.js"></script>
</body>
</html>
and my JavaScript file:
//get the start element
var start = document.getElementById("start");
//register listener
start.addEventListener('click', startCountDown(), false);
function startCountDown()
{
//alert("Here!");
//creat the list of 10 movies
var myTenMovies = ["Breaveheart", "Mars Attacks", "Startship Troopers", "Jurassic Park", "Terminator 2",
"Austion Powers: Gold Member", "Fifth Element", "Bram Stocker's Dracula",
"Shashank Redemption", "LOTR: Return Of The King"];
//get the location in the DOM to add each movie
var tableLocation = document.getElementById("movieList");
//create a timer function
var countdown = setInterval(insertNextMovie, 750);
//keep track of the index
var index = 0;
var movieRankIndex = 10;
//alert("1");
//function to add to DOM
function insertNextMovie()
{
//alert("Here3!");
//create a text nodes
var movie = document.createTextNode(myTenMovies[index]);
var rank = document.createTextNode(movieRankIndex);
//create <td> element
var rankData = document.createElement("td");
var movieTitle = document.createElement("td");
var row = document.createElement("tr");
//add text to td
rankData.appendChild(rank);
movieTitle.appendChild(movie);
//fill the row with data
row.appendChild(rankData);
row.appendChild(movieTitle);
if(movieRankIndex == 1)
{
row.setAttribute("bgcolor", "yellow");
//row.setAttribute("align", "right");
}
//add td to DOM table
tableLocation.appendChild(row);
//append index
index++;
movieRankIndex--;
if(index == 10)
{
clearInterval(countdown);
}
}
//alert("2");
}
I want to make a simple JavaScript countdown program. For some reason the countdown triggers without me having to click the start link.
<!DOCTYPE HTML>
<html>
<head>
<title id = "titleElement">My Top Ten Movies</title>
</head>
<body>
<p>Countdown Suren's favorite ten movies
<a href="" id = "start"> Start </a>
</p>
<table>
<thead>
<tr>
<td>Number</td>
<td>Movie</td>
</tr>
</thead>
<tbody id = "movieList">
</tbody>
</table>
<script src="js/practice.js"></script>
</body>
</html>
and my JavaScript file:
//get the start element
var start = document.getElementById("start");
//register listener
start.addEventListener('click', startCountDown(), false);
function startCountDown()
{
//alert("Here!");
//creat the list of 10 movies
var myTenMovies = ["Breaveheart", "Mars Attacks", "Startship Troopers", "Jurassic Park", "Terminator 2",
"Austion Powers: Gold Member", "Fifth Element", "Bram Stocker's Dracula",
"Shashank Redemption", "LOTR: Return Of The King"];
//get the location in the DOM to add each movie
var tableLocation = document.getElementById("movieList");
//create a timer function
var countdown = setInterval(insertNextMovie, 750);
//keep track of the index
var index = 0;
var movieRankIndex = 10;
//alert("1");
//function to add to DOM
function insertNextMovie()
{
//alert("Here3!");
//create a text nodes
var movie = document.createTextNode(myTenMovies[index]);
var rank = document.createTextNode(movieRankIndex);
//create <td> element
var rankData = document.createElement("td");
var movieTitle = document.createElement("td");
var row = document.createElement("tr");
//add text to td
rankData.appendChild(rank);
movieTitle.appendChild(movie);
//fill the row with data
row.appendChild(rankData);
row.appendChild(movieTitle);
if(movieRankIndex == 1)
{
row.setAttribute("bgcolor", "yellow");
//row.setAttribute("align", "right");
}
//add td to DOM table
tableLocation.appendChild(row);
//append index
index++;
movieRankIndex--;
if(index == 10)
{
clearInterval(countdown);
}
}
//alert("2");
}
Share
Improve this question
edited Feb 4, 2023 at 13:07
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jul 15, 2012 at 5:44
SurenSuren
211 silver badge2 bronze badges
1 Answer
Reset to default 10That's because startCountDown()
calls the function startCountDown
. Remove the parentheses, and it'll work.
本文标签: addEventListener in Javascript triggers the click event automaticallyStack Overflow
版权声明:本文标题:addEventListener in Javascript triggers the click event automatically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741305661a2371353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论