0

我是 Jquery 插件创建的新手。以下 jquery 插件是使用 jQuery Boilerplate 创建的。它只是进行计数并在计数完成时通知。
我想通过将计数器设置为 0 来重新开始计数;我不明白如何调用该重置功能

  ;(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));

准备好文件 ::

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

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

4

1 回答 1

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/

于 2012-06-01T05:28:25.897 回答