jQuery的Countdown插件 v0.2

| 12 comments 2008-03-10 19:32:02

写了一个简单的倒计时(countdown)插件,用法非常简单:

$('div.countdown').countdown({seconds: 3,callback: 'helloWorld()'});

上例中,这个插件会在div.countdown中插入3秒钟的倒计时,直到0为止并调用helloWorld()这个函数。

这个插件有两个选项:

  • seconds - 从多少秒开始倒计时;
  • callback - 可选,倒计时结束时执行的回调函数。

目前的版本是0.2,下载地址如下:

这个插件在jQuery Plugins上的项目站点,觉得好用可以投上一票 :)。

更新记录:

0.2。加入了stop()方法,可以停止一个正在进行中的倒计时。

12 comments so far

  1. cssrain 2008-03-29 20:53:59

    嘿嘿

  2. xiong 2008-06-28 04:58:49

    very nice script! It would be better if follows the standard format:

    $('#div').countdown('3', function(){});

    just my 2c.

    thanks

  3. skyblue 2008-07-12 23:41:27

    建议搞个加强版..

    可以把秒格式成分钟显示的

  4. lei 2008-11-20 11:58:32

    顶~~~~~~~~~~~~~

  5. Trent 2008-12-09 06:17:25

    Thanks!!

    A mod for you.

    //Display time in mins secs format :)

    /**

    * recursive countdown

    */

    window.setTimeout(

    function() {

    mins = Math.floor(mins_prep/60);

    secs = mins_prep - (mins*60);

    jQuery(obj).html(String(mins+' mins '+secs+' secs'));

    --options.seconds;

    jQuery(obj).countdown(options);

    }

    , 1000

    );

  6. Trent 2008-12-09 06:19:19

    Woops typos in last post emu.gif

    //Display time in mins secs format :)

    /**

    * recursive countdown

    */

    window.setTimeout(

    function() {

    mins = Math.floor(options.seconds/60);

    secs = options.seconds - (mins*60);

    jQuery(obj).html(String(mins+' mins '+secs+' secs'));

    --options.seconds;

    jQuery(obj).countdown(options);

    }

    , 1000

    );

  7. 丁宇 2008-12-09 17:29:11

    @Trent: thanks for the submit! smile_1.gif

  8. Javi 2009-01-09 17:42:33

    //With "clock format": 04:30

    window.setTimeout(

    function() {

    mins = Math.floor(options.seconds/60);

    secs = options.seconds - (mins*60);

    if (mins.toString().length < 2) {

    minsLabel = '0' + mins;

    }

    else {

    minsLabel = mins;

    }

    if (secs.toString().length < 2) {

    secsLabel = '0' + secs;

    }

    else {

    secsLabel = secs;

    }

    jQuery(obj).html(String(minsLabel+':'+secsLabel));

    --options.seconds;

    jQuery(obj).countdown(options);

    }

    , 1000

    );

  9. wiki 2009-02-23 23:22:04

    To become able and be more flexible in formating, I've added a format_function callback.

    /**

    * recursive countdown

    */

    window.setTimeout(

    function() {

    if( options.format_function ) {

    jQuery(obj).html( options.format_function( options.seconds ) );

    } else {

    jQuery(obj).html(String(options.seconds));

    }

    --options.seconds;

    jQuery(obj).countdown( options );

    }

    , 1000

    );

  10. Hatem BEN RAIS 2009-08-18 22:55:27

    Hey,

    Nice plugin, thanks!

    Here is a modified version that displays countdown in HH:MM:SS format:

    jQuery.fn.CountDown = function(options) {

    /**

    * app init

    */

    if(!options) options = '()';

    if(jQuery(this).length == 0) return false;

    var obj = this;

    if (!options.digits || options.digits <= 0 || options.digits == 'undefined')

    options.digits = 2 ;

    /**

    * break out and execute callback (if any)

    */

    if(options.seconds < 0 || options.seconds == 'undefined')

    {

    if(options.callback) eval(options.callback);

    return null;

    }

    /**

    * add leading zeros to number

    */

    function pad_digits(nb, totalDigits) {

    nb = nb.toString();

    var pd = '';

    if (totalDigits > nb.length)

    for (i=0; i < (totalDigits - nb.length); i++)

    pd += '0';

    return pd + nb.toString();

    };

    /**

    * recursive countdown

    */

    window.setTimeout(

    function() {

    jQuery(obj).html(String(pad_digits(options.hours, options.digits) + ' : ' + pad_digits(options.minutes, options.digits) + ' : ' + pad_digits(options.seconds, options.digits)));

    options.seconds -= 1 ;

    if (options.seconds < 0) {

    options.seconds = 59;

    options.minutes -= 1;

    if (options.minutes < 0) {

    options.minutes = 59;

    options.hours -= 1;

    if (options.hours < 0) {

    options.hours = 23;

    }

    }

    }

    jQuery(obj).CountDown(options);

    }

    , 1000

    );

    /**

    * return null

    */

    return this;

    }

    Simple usage:

    $('#countdown').CountDown({hours:3, minutes:38, seconds:15, digits:2});

  11. hiro 2010-03-20 00:50:05

    这个插件如何,xhtml代码很简单:

    <label>4</label>

    jQuery代码:

    ;(function($) {

    $.fn.timer = function(callback) {

    var _this = this,

    oldTimer = _this.text(),

    timer = oldTimer;

    var id = setInterval(function() {

    timer--;

    if(timer == 0) {

    clearInterval(id);

    };

    _this.text(timer);

    }, 1000);

    setTimeout(function() {

    if(callback) {

    callback();

    };

    }, oldTimer*1000);

    };

    })(jQuery);

    jQuery(function($) {

    $('label').timer(function() {

    alert('倒计时结束');

    });

    });

    欢迎回访我的博客,给我提点意见啊!!

  12. 丁宇 2010-03-22 10:30:42

    @hiro 看了你的网站,头部的鼠标效果很有意思 clin_oeil.gif

(Support Gravatar)
  • angel.gif
  • glasses.gif
  • hum.gif
  • sad.gif
  • caresse.gif
  • sick.gif
  • angry.gif
  • zip.gif
  • gun.gif
  • emu.gif
  • big_smile.gif
  • clin_oeil.gif
  • devil.gif
  • wahou.gif
  • confus.gif
  • mad.gif
  • larme.gif
  • wave.gif
  • scare.gif
  • lang_1.gif
  • ask.gif
  • xd.gif
  • eye_up.gif
  • mdr.gif
  • smile_1.gif
  • lang_2.gif
  • zzz.gif
  • bad_smile.gif
  • jet.gif
  • smile_2.gif
  • love.gif

About

我在厦门拍的照片

丁宇(Felix Ding),电脑Geek,狂热的爱书和爱乐分子,99年迷上网页设计,并从此一发不可收。现在在上海做用户体验/产品设计咨询。Email: felixding[AT]gmail.com。

订阅到RSS