LearnFast.Ninja

JavaScript setTimeout parameters passing function

Code and delay are the setTimeout function parameters. code can be string or anonymous function, delay is a number on miilisecondes.To pass parameters to the code parameter on setTimout, you need to do something like function(){ another_function(params)}

Working example:

<html>

<head>

    <title>javascript settimeout parameters passing function</title>

    <script type="text/javascript">

        var singleSetTimeOut = null;

        function count_time(jumps, delay, current_time) {

            document.getElementById("CountDiv").innerHTML = current_time;

            if (singleSetTimeOut != null) {

                clearTimeout(singleSetTimeOut);

            }

            singleSetTimeOut = setTimeout(function () {

                count_time(jumps, delay, parseInt(jumps + current_time));

            }, delay);

        }

    </script>

</head>

<body>

    <input type="button" onclick="count_time(1, 500, 0);" value="Up on 1 every time" />

    <input type="button" onclick="count_time(10, 500, 0);" value="Up on 10 every time" />

    <input type="button" onclick="count_time(100, 500, 0);" value="Up on 100 every time" />

    <div id="CountDiv">

        Click on one of the buttons to count

    </div>

</body>

</html>


Result:


Tags

javascript

settimeout delay