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
Add a ment  | 

1 Answer 1

Reset to default 10

That's because startCountDown() calls the function startCountDown. Remove the parentheses, and it'll work.

本文标签: addEventListener in Javascript triggers the click event automaticallyStack Overflow