Javascript 在PHP和JQuery中更新time ago函数而不刷新页面
我想让我的time ago函数在不刷新页面的情况下自动更新它,例如:当我上传一张图片时,它在图片下方返回19秒前它将保持原样,不会改变,但当我刷新页面时,它将在1分钟前或其他任何时间 这是PHP中的Time-Ago函数,我从css技巧中获得:Javascript 在PHP和JQuery中更新time ago函数而不刷新页面,javascript,php,jquery,Javascript,Php,Jquery,我想让我的time ago函数在不刷新页面的情况下自动更新它,例如:当我上传一张图片时,它在图片下方返回19秒前它将保持原样,不会改变,但当我刷新页面时,它将在1分钟前或其他任何时间 这是PHP中的Time-Ago函数,我从css技巧中获得: function ago($time){ $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths =
function ago($time){
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] ago ";
}
函数前($time){
$periods=数组(“秒”、“分钟”、“小时”、“日”、“周”、“月”、“年”、“十年”);
$length=数组(“60”、“60”、“24”、“7”、“4.35”、“12”、“10”);
$now=时间();
$difference=$now-$time;
$tense=“ago”;
对于($j=0;$difference>=$length[$j]&&$j
如何在不使用jquery刷新页面的情况下进行更改?将时间戳发送到JavaScript并从那里自动更新之前的时间,而不是查找时间差。我不能使用AJAX进行更改吗?我建议不要使用它,因为JavaScript可以做得很好,而且(1)会更平滑,(2)减少对服务器的点击。如果您接受我的建议,请使用jQuery插件。