使用“jQuery Boilerplate”的Jquery countup插件

时间:2012-06-01 04:59:37

标签: javascript jquery jquery-plugins

我是Jquery插件创建的新手。以下jquery插件是使用jQuery Boilerplate创建的。它只是进行计数并在计数结束时通知 我希望通过将counter设置为0来重新​​启动count-up; 我不明白如何调用重置函数

  ;(function ( $, window, undefined ) {


        var pluginName = 'countup',
            document = window.document,
            defaults = {
                countSince: new Date(),
                countUpTo:120,
                notifyAfter:110,
                onExpire:function() {

                },          
            };

        // The actual plugin constructor
        function Plugin( element, options ) {
            this.element = element;
            this.options = $.extend( {counter:0}, defaults, options) ;
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
        }

        Plugin.prototype.init = function () {
            this.tick();

        };
        Plugin.prototype.reset = function () {
            this.options.counter = 0;

        };
        Plugin.prototype.tick = function () {

                if (this.options.counter > this.options.countUpTo) {
                    //timer expired
                    this.options.onExpire($(this.element));
                }
                else {
                    if (this.options.counter > this.options.notifyAfter) {
                        $(this.element).find('span').html('<strong style="font-size: 15px; color:#ff0000;">' + this.options.counter+ ' seconds</strong>');
                    }
                    else {
                        $(this.element).find('span').html('<strong style="font-size: 15px; color:#3366ff">' + this.options.counter + ' seconds</strong>');
                    }

                    setTimeout(function() {
                        this.options.counter += 1;
                        this.tick();
                    }, 1000);//calling tick function again
                }       

        };  

        $.fn[pluginName] = function ( options ) {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
                }
            });
        };

    }(jQuery, window));

on document ready ::

$('#countdown').countup({
                    onExpire:function() {
                        alert('hi');
                    },
                    countSince:new Date(),//count from this
                    countUpTo:30,//seconds from the countSince to expire
                    notifyAfter:20
})

之后我想在$('#countdown')上调用reset()函数。怎么做?或者有更好的方法来编写上面的代码?请在这里给我一些帮助。

1 个答案:

答案 0 :(得分:0)

您创建的样板代码非常复杂。以下HTML和JavaScript(以及jQuery)将完成从0开始的向上计数器,直到由var countTo定义的秒数,此时它将打印存储在var successMsg中的消息。重置按钮将计数器重新启动为零。

HTML:

<div id="countdown"></div>
<input id="countreset" value="Reset" type="button" />​

JavaScript的:

var countTo = 5; // Time to count to
var successMsg = 'hi'; // Replace count with this message at max number of seconds
function countUp() {
    var countdown = $('#countdown');
    // Turn contents into integer
    var current = parseInt(countdown.html());
    // Increment seconds
    var next = current+1;
    if (next >= countTo) {
        // If at max seconds, replace with message
        countdown.html(successMsg);                
    } else {
        // Replace seconds with next second
        countdown.html(next);
        setTimeout(countUp, 1000);
    }
}
function startCountdown() {
    var countdown = $('#countdown');
    // Set to zero seconds
    countdown.html('0');

    // Start counting
    setTimeout(countUp, 1000);
}
$(document).ready(function() {
    // Start the countdown
    startCountdown();
    $('#countreset').click(function() {
        // On reset button .click(), start countdown
        startCountdown();
    });
});​

我已将解决方案放在jsFiddle:http://jsfiddle.net/TxVnS/2/

<强>更新 允许变量计数器ID的解决方案如下:http://jsfiddle.net/TxVnS/6/