Javascript简单timeago脚本设置为跨类或标签ID

时间:2016-05-10 01:09:40

标签: javascript html timeago

我的脚本最初取自Stackoverflow question thread。此脚本将允许用户注意最后一次修改页面的时间。我希望脚本由其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";

}

3 个答案:

答案 0 :(得分:0)

之前我没有看到您的代码,因此它是您当前代码的解决方案。

var pa = document.getElementById('lbl_timeago').parentNode;
        setInterval(function(){
            pa.innerHTML = timeSince(date);
            },5000);
        pa.innerHTML = timeSince(date);

没有太大变化。我已经测试过,它应该可以工作。

在jQuery中

$('#puthere').parent();

答案 1 :(得分:0)

你在找这样的东西吗?

vec4 texColor = texture2D(u_texture, v_texCoords);
gl_FragColor = vec4(mix(vec3(1.0), v_color.rgb, texColor.rrr), v_color.a * texColor.a);

答案 2 :(得分:0)

我已通过此代码解决了这个问题。

<强> HTML

 This page is updated <span class="timeago_span"> </span> ago

<强>的JavaScript

var date = new Date();
var finaltime = timeSince(date);
setInterval(function(){
  document.getElementsByClassName('timeago_span')[0].innerHTML = timeSince(date);
},5000);
document.getElementsByClassName('timeago_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 Math.floor(seconds) + " seconds";
  return  " a while "

}