jQuery在自定义插件上实现destroy方法

时间:2015-09-09 02:19:18

标签: javascript jquery jquery-plugins destroy jquery-events

我正在开发一个内部工具的插件,用于跟踪处理特定任务所需的时间。我提供了它所用时间的视觉参考,但是使用数据库中的时间戳来捕获真值。

在我正在处理的插件中,我正在尝试构建一个将计数器重置为零的销毁函数。

我是如何做到的只是破坏了这个插件的原始元素。我想这会让垃圾收集器清理其他任何东西。

我认为这不起作用的原因是因为我添加了一行,随着时间的推移更新了document title。当我“摧毁”它时,标题行为都很时髦,就像计时器仍在继续,即使我启动它的实际计时器被重置,就像我预期的那样。

如何判断这是否被正确销毁?

http://jsfiddle.net/9bf4jmao/3/

// BEGIN THE PLUGIN
var globalContainer;
(function($) {

$.fn.upCount = function(options, callback) {
    var settings = $.extend({
        startTime: null,
        offset: null,
        reset: null,
        resume: null
    }, options);

    // Save container
    var container = this;
    globalContainer = container.parent().html();

    // get the current date
    var currentDate = function() {
        // get client's current date
        var date = new Date();

        // turn date to utc
        var utc = date.getTime() + (date.getTimezoneOffset() * 60000);

        // set new Date object
        var new_date = new Date(utc + (3600000 * settings.offset))

        return new_date;
    };

    // Define some global vars
    var original_date = currentDate();
    var target_date = new Date('12/31/2020 12:00:00'); // Count up to this date

    // Are we resetting our counter?
    if(settings.reset){
        reset();
    }

    // Do we need to start our counter at a certain time if we left and came back?
    if(settings.startTime){
        resumeTimer(new Date(settings.startTime));
    }

    // Reset the counter by destroying the element it was bound to
    function reset() {
        var timerContainer = $('[name=timerContainer]');
        timerContainer.empty().append(globalContainer).find('.time').empty().append('00');
    }

    // Given a start time, lets set the timer
    function resumeTimer(startTime){
       original_date = startTime;
    }

    // Start the counter
    function countUp() {

        // Set our current date
        var current_date = currentDate(); 

        // difference of dates
        var difference = current_date - original_date;
        //console.log(original_date)

        if (current_date >= target_date) {
            // stop timer
            clearInterval(interval);
            if (callback && typeof callback === 'function') callback();
            return;
        }

        // basic math variables
        var _second = 1000,
            _minute = _second * 60,
            _hour = _minute * 60,
            _day = _hour * 24;

        // calculate dates
        var days = Math.floor(difference / _day),
            hours = Math.floor((difference % _day) / _hour),
            minutes = Math.floor((difference % _hour) / _minute),
            seconds = Math.floor((difference % _minute) / _second);

        // fix dates so that it will show two digets
        days = (String(days).length >= 2) ? days : '0' + days;
        hours = (String(hours).length >= 2) ? hours : '0' + hours;
        minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
        seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;

        // based on the date change the refrence wording
        var ref_days = (days === 1) ? 'day' : 'days',
            ref_hours = (hours === 1) ? 'hour' : 'hours',
            ref_minutes = (minutes === 1) ? 'minute' : 'minutes',
            ref_seconds = (seconds === 1) ? 'second' : 'seconds';

        // set to DOM
        container.find('.days').text(days);
        container.find('.hours').text(hours);
        container.find('.minutes').text(minutes);
        container.find('.seconds').text(seconds);

        container.find('.days_ref').text(ref_days);
        container.find('.hours_ref').text(ref_hours);
        container.find('.minutes_ref').text(ref_minutes);
        container.find('.seconds_ref').text(ref_seconds);

        // Update our title
        document.title = 'Task Tracker - ' + days +'.'+hours+':'+minutes+':'+seconds;
    };

    // start
    interval = setInterval(countUp, 1000);
};

})(jQuery);
 // END THE PLUGIN

我正在做的是调用$('.countdown').upCount({reset:true});以便销毁(或者我想破坏它)

更新 我更新了小提琴,表明即使在插件被“销毁”之后,该元素仍然会被更新,这表明某些事情本来就没有完成。

更新2 当我在聊天中提出这个问题时,我被告知我interval = setInterval(...) but scope block it

我不太清楚该怎么做,从那时起就无法得到答案。

1 个答案:

答案 0 :(得分:0)

最好的猜测是我没有看到你清除你的间隔:

StringBuilder

但是,您需要在脚本顶部初始化该变量,我没有看到。

// in your reset function, you should be stopping the interval:
clearInterval(interval);