Akbar
Akbar

Reputation: 3

How do Countdown can be repeatedly?

Try to open jsfiddle.net/3hjLb/1/ then click on "Try it" wait until the 0 and click "Try it" and see happen,

How do the "0" it could go back to 10 if you click on "Try it" a second time?

Upvotes: 0

Views: 63

Answers (3)

mehdi
mehdi

Reputation: 1755

try to set counter value in onclick event : DEMO

<button onclick="counter = 10;countDown()">Try it</button>

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

reset your counter variable to 10 before call onclick function

like this

<button onclick="javascript:counter=10; countDown()">Try it</button>

See JS Fiddle

Upvotes: 0

Guy Dafny
Guy Dafny

Reputation: 1829

using "if" condition is one option:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title> Bla! </title>
        <script type='text/javascript'>
            var counter = 10;
            function CountDown(obj) {
                if (--counter < 0) counter = 10;
                obj.innerHTML = "counter=" + counter;
            }
        </script>
    </head>
    <body>
        <button onclick='CountDown(this)'> Click to countdown (10) </button>
    </body>
</html>

Upvotes: 0

Related Questions