javascript - $.fn.fixTime 不起作用

标签 javascript jquery extend

http://jsfiddle.net/AVLzH/14/

我正在尝试构建我的第一个 jQuery 扩展函数,它将采用 YYYYMMDDHHMMSS 中的时间格式。并将其转换为可读的内容,例如 Just a moment ago2 hours ago在将我的代码变成扩展函数之前,我的代码运行得非常好。

Sidebar: yes I know its better to use Server-side code to get the current time, this is just for example purposes

当我调用该函数时,它位于一个获取 datetime 的链中。来自所有 <time> 的属性元素,并用返回的内容切换文本,并将旧文本设置为 data-tooltip .

有很多代码,因此最好查看 jsfiddle 页面:

http://jsfiddle.net/AVLzH/14/

jsLint 返回以下错误:

Error:
Expected an assignment or function call and instead saw an expression.
});

Expected '(end)' and instead saw '}'.
})( jQuery );

Implied global: jQuery 3, console 19,20,22,23,40,50,75

是的,基本上我不知道该怎么办。

感谢您提前提供的帮助!

http://jsfiddle.net/AVLzH/14/

PS - 不小心把旧代码放在那里..更改了链接

感谢您迄今为止的所有帮助。jsLint 不再返回任何错误,但它无法正确执行。

最佳答案

返回后的部分是错误的

 return returning;
    };
});

})( jQuery );

,它应该是:

  return returning;
    };
}( jQuery ));

尝试 Jsfiddle 上的 JSLint 按钮来检查您的语法。

(function( $ ) {

    jQuery.fn.fixTime = function(activityTime) {



            var currentTime = new Date(),
                month  = currentTime.getMonth() + 1,
                day    = currentTime.getDate(),
                year   = currentTime.getFullYear(),
                hour   = currentTime.getHours(),
                minute = currentTime.getMinutes(),
                second = currentTime.getSeconds(),
                activityTime = new Date(parseInt(this.attr('datetime'), 10)),
                masterTime = (year*10000000000) + (month*100000000) + (day*1000000) + (hour*10000) + (minute*100) + (second * 1),
                timeDifference = masterTime - activityTime,
                returning;

            console.log("Current Time: " + month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second);    
            console.log("Current Time: " + year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second);

            console.log("Current Time:  " + masterTime);
            console.log("Activity Time: " + activityTime, this);

        console.log(this.attr('datetime'))
            console.log(new Date(20120211103802))
            // Change Time 
            timeDifference = timeDifference + 0;

            // YYYYMMDDHHMMSS

            //             60 - 1 Min
            //           6000 - 1 Hour
            //         240000 - 1 Day
            //        7000000 - 1 Week
            //       30000000 - 1 Month
            //    10000000000 - 1 Year

            // YYYYMMDDHHMMSS

            console.log("Time Difference: " + timeDifference);

            if (0 <= timeDifference && timeDifference < 60) {
                returning = "Just a moment ago";
            } else if (60 <= timeDifference && timeDifference < 120) {
                returning = "A minute ago";
            } else if (120 <= timeDifference && timeDifference < 6000) {
                timeDifference = Math.floor(timeDifference/100);
                returning = timeDifference + " minutes ago";
            } else if (6000 <= timeDifference && timeDifference < 20000) {
                console.log("1 hour ago");
            } else if (20000 <= timeDifference && timeDifference < 240000) {
                timeDifference = Math.floor(timeDifference/10000);
                returning = timeDifference + " hours ago";
            } else if (240000 <= timeDifference && timeDifference < 2000000) {
                returning = "Yesterday";
            } else if (2000000 <= timeDifference && timeDifference < 7000000) {
                timeDifference = Math.floor(timeDifference/1000000);
                returning = timeDifference + " days ago";
            } else if (7000000 <= timeDifference && timeDifference < 14000000) {
                return "A week ago";
            } else if (14000000 <= timeDifference && timeDifference < 30000000) {
                timeDifference = Math.floor(timeDifference/7000000);
                returning = timeDifference + " weeks ago";
            } else if (30000000 <= timeDifference && timeDifference < 200000000) {
                returning = "A month ago";
            } else if (200000000 <= timeDifference && timeDifference < 10000000000) {
                timeDifference = Math.floor(timeDifference/100000000);
                returning = timeDifference + " months ago";
            } else if (10000000000 <= timeDifference && timeDifference < 20000000000) {
                returning = "A year ago";
            } else if (20000000000 <= timeDifference && timeDifference < 1000000000000) {
                timeDifference = Math.floor(timeDifference/10000000000);
                returning = timeDifference + " years ago";
            } else {
                console.error("Error Calculating"); // Only when less than zero or longer than 100 years
                returning = "undefined";
            }

            return returning;
        };

}( jQuery ));

(function() {

    var times = $('time');

    times.each(function() {
        var beforeTime = $(this).text();
   //     var betterTime = new Date($(this).attr('datetime'));

        var betterTime = $(this).fixTime();

        $(this).text(betterTime).attr('data-tooltip', beforeTime);
    });

})();

关于javascript - $.fn.fixTime 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272654/

相关文章:

javascript - 如何在jquery High-chart中填充多表数据

javascript - 更改焦点输入元素的背景颜色

jQuery 扩展带参数的方法?

jQuery 通过索引获取数组的值

javascript - 需要字母和数字 - 正则表达式

javascript - 将 jQuery 与 Webpack 结合使用

javascript - 打印 CSS : How do I open a new tab of the current page and append CSS?

inheritance - 用例中的扩展与泛化

javascript - 直接在 jQuery 对象上定义实用函数有什么好处吗?

jquery - 解决 CSS 滚动条宽度