[英]$.fn.fixTime not working
我正在嘗試構建我的第一個jQuery擴展函數,該函數將采用YYYYMMDDHHMMSS
的時間格式,並將其轉換為可讀的內容,例如Just a moment ago
或2 hours ago
我的代碼在完成擴展功能之前運行良好。
邊欄:是的,我知道最好使用服務器端代碼來獲取當前時間,這只是出於示例目的
當我調用該函數時,它會從所有<time>
元素中獲取datetime
屬性,並使用返回的內容切換文本並將舊文本設置為data-tooltip
。
有很多代碼,因此最好查看jsfiddle頁面:
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
是的,基本上我不知道該怎么辦。
感謝您提前提供的幫助!
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);
});
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.