开发者

jquery countdown

I am new to 开发者_运维知识库programming jquery php. I want to countdown a php time variable in jquery. For example time= 31233. How could i count down this like mm:ss?

Please help thanks


Try this:

$(function() {
    var defaultTimer = 400, // Default amount of seconds if url variable is not found
        callback = function() {
            // Will be executed when the timer finishes
            alert("Time is up!");   
        };

    var counter = 1, timer, 
        match = document.location.href.match(/[\?|&]timer=(\d+)/i),
        totalTime = match ? match[1] : defaultTimer;

    timer = setInterval(function() {
        if (totalTime != -1 && !isNaN(totalTime)) {
            val = 'Time left: ' + (function() {
                var m = Math.floor(totalTime / 60);
                if (m < 10) {
                    return '0'.concat(m);
                } else {
                    return m;
                }
            })() + ':' + (function() {
                var s = totalTime % 60;
                if (s < 10) {
                    return '0'.concat(s);
                } else {
                    return s;
                }
            })();

            $('#counter').html(val);
            totalTime--;
        } else {
            window.clearInterval(timer);
            timer = null;
            callback();
        }
    }, 1000);
});

You can find an example here: http://jsfiddle.net/hHD4w/


Check this out...

http://code.google.com/p/jquery-countdown/

... and the demo...

http://jquery-countdown.googlecode.com/svn/trunk/index.html

I hope this helps.
Hristo


Here you go

<span id="countdown"></span>

<script>
var timer = 31233;

setInterval( function() {
    var minute = Math.floor(timer / 60);
    var second = '00' + (timer % 60);
    var val = minute + ':' + second.substring(second.length - 2);
    $('#countdown').html(val);   
    timer--;
}, 1000);
</script>

fixed formatting - http://jsfiddle.net/3NYY9/8/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜