Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery(或类似工具)显示用户(客户端)进入或刷新页面的时间_Javascript_Jquery_Html_Timer_Timeago - Fatal编程技术网

Javascript 使用jQuery(或类似工具)显示用户(客户端)进入或刷新页面的时间

Javascript 使用jQuery(或类似工具)显示用户(客户端)进入或刷新页面的时间,javascript,jquery,html,timer,timeago,Javascript,Jquery,Html,Timer,Timeago,目前这是结果,但计时器不会变为大约一分钟前或5分钟前,等等。它总是以实际时间为准。没有找到适合我的解决方案 //timeago by jQuery (function timeAgo(selector) { var templates = { prefix: "", suffix: " ago", seconds: "less than a minute", minute: "about a minute", minutes: "

目前这是结果,但计时器不会变为大约一分钟前或5分钟前,等等。它总是以实际时间为准。没有找到适合我的解决方案

//timeago by jQuery
(function timeAgo(selector) {

  var templates = {
      prefix: "",
      suffix: " ago",
      seconds: "less than a minute",
      minute: "about a minute",
      minutes: "%d minutes",
      hour: "about an hour",
      hours: "about %d hours",
      day: "a day",
      days: "%d days",
      month: "about a month",
      months: "%d months",
      year: "about a year",
      years: "%d years"
  };
  var template = function(t, n) {
      return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
  };

  var timer = function(time) {
      if (!time)
          return;
      time = time.replace(/\.\d+/, ""); // remove milliseconds
      time = time.replace(/-/, "/").replace(/-/, "/");
      time = time.replace(/T/, " ").replace(/Z/, " UTC");
      time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
      time = new Date(time * 1000 || time);

      var now = new Date();
      var seconds = ((now.getTime() - time) * .001) >> 0;
      var minutes = seconds / 60;
      var hours = minutes / 60;
      var days = hours / 24;
      var years = days / 365;

      return templates.prefix + (
              seconds < 45 && template('seconds', seconds) ||
              seconds < 90 && template('minute', 1) ||
              minutes < 45 && template('minutes', minutes) ||
              minutes < 90 && template('hour', 1) ||
              hours < 24 && template('hours', hours) ||
              hours < 42 && template('day', 1) ||
              days < 30 && template('days', days) ||
              days < 45 && template('month', 1) ||
              days < 365 && template('months', days / 30) ||
              years < 1.5 && template('year', 1) ||
              template('years', years)
              ) + templates.suffix;
  };

  var elements = document.getElementsByClassName('timeago');
  for (var i in elements) {
      var $this = elements[i];
      if (typeof $this === 'object') {
          $this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
      }
  }
  // update time every minute
  setTimeout(timeAgo, 60000);

})();


//Uhrzeit
a = new Date ();
b = a.getHours();
c = a.getMinutes();
d = a.getSeconds();
zeit = b+':'+c+':'+d;
document.getElementById("uhrzeit").innerHTML = zeit;
这就是我想在html文档中使用它的方式,当我设置日期时,或者当我使用uhrzeit时,它会起作用,uhrzeit是一个占用实际时间的小脚本,但我想要客户端打开页面的时间。对不起,有什么不便吗

<div class="card-footer">
          <small class="text-muted"><time class="timeago" datetime="uhrzeit">December 17, 2019</time></small>
        </div>

您的错误可能在以下几行:

对于元素中的var i{ var$this=元素; 如果$this的类型==='object'{ $this.innerHTML=计时器$this.getAttribute'title'| |$this.getAttribute'datetime'; } } 您是否更改了标题或日期时间属性?因为,如果不是,您总是向计时器函数发送相同的值

编辑: 添加用户最后一次作为时间元素的属性输入的时间

像这样

11年前
只需在a=新日期上指定所需的日期;请参见:上的可用选项,但没有选择用户进入网站的时间。当我使用f=新日期时;document.getElementByIdclienttime.innerHTML=f;然后它总是显示不到一分钟的时间。哦,我没有改变一些东西。它是直接从timeago网站上出来的。我怎样才能修复它?我希望它花费用户进入页面的时间,然后在页面上显示时间。您需要在11年前这样做,将title或datetime属性添加到time元素