开发者

Countdown timer on Label for 30 seconds using Javascript

开发者 https://www.devze.com 2023-03-11 12:31 出处:网络
I use the following example Redirect to redirect to a page. What i need is inside the image i would like to have a label or some text which should show count down from 30(secs) to 0(secs). I need java

I use the following example Redirect to redirect to a page. What i need is inside the image i would like to have a label or some text which should show count down from 30(secs) to 0(secs). I need javasc开发者_运维问答ript for this requirement.

Any help is appreciated


Hi include this before your existing DIV

<div id="myCounter">
</div>

Write the following Script

<script type="text/javascript">

        var milisec = 0
        var seconds = 30
        document.getElementById("myCounter").innerHTML = '30';

        function display() {
            if (milisec <= 0) {
                milisec = 9
                seconds -= 1
            }
            if (seconds <= -1) {
                milisec = 0
                seconds += 1
            }
            else
                milisec -= 1
            document.getElementById("myCounter").innerHTML = seconds;
            setTimeout("display()", 100)
        }
        display() 

    </script>

Adjust your DIV as per your need in the design by setting the position to absolute


<span id="myCounter"></span>

<script>
function counter (count) {
    if (count > 0) {
        document.getElementById("myCounter").innerHTML = count;
        window.setTimeout(function() {counter(count-1)}, 1000);
    } else {
        window.location.href = "/redirected-page/";
    }
}

counter(30);
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消