我有一个最初取自Stackoverflow 问题线程的脚本。该脚本将允许用户注意页面最后一次被修改的时间。我希望脚本由其 SPAN CLASS 或 LABEL ID 而不是其 DIV SPAN 设置。任何帮助将非常感激。谢谢你。:)
这是我到目前为止所拥有的:
HTML
此页面已更新
JavaScript
var date = new Date();
var finaltime = timeSince(date);
setInterval(function(){
document.getElementsByTagName('span')[0].innerHTML = timeSince(date);
},5000);
document.getElementsByTagName('span')[0].innerHTML = timeSince(date);
function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds / 120);
if (interval >= 2){
return interval + "minutes";
}
interval = Math.floor(seconds / 60);
if (interval == 1) {
return interval + " minute";
}
return "a while";
}