![](/img/trans.png)
[英]Can I load jQuery dynamically to a local variable rather than to window?
[英]Using jQuery, how can I ajax load text into multiple .classes rather than #id's?
在我的网站上,我有一个评论表格,告诉您多久前发布评论。
下面的代码仅每10秒重新加载一次包含日期的跨度,而不是整个注释div(以防止出现页面滞后)
$(function(){
setInterval(
function(){
var id = $('.date').html();
$('.date').load('/comdate.php?id=' + id); }
,10000);
});
我的功能无法正常工作,我真的不想为每个评论的日期字段都设置单独的功能。 请有人指出我的问题吗? 谢谢
编辑
我在javascript中使用它来将时间更改为可读的日期
function prettyDate(time){
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var date = prettyDate(this.title);
if ( date )
jQuery(this).text( date );
});
};
这将返回GMT + 1,因为那是我所在的位置。 如何更改此功能,以使javascript时区也成为GMT?
对我来说,我将更改ajax请求以仅发送一次unix时间跨度,并且我将使用javascript进行所有计算。
var periods = ["second", "minute", "hour", "day", "week", "month", "year", "decade"];
var lengths = ["60","60","24","7","4.35","12","10"];
function nicetime(user_date)
{
if(typeof user_date == "undefined" ) {
return "No date provided";
}
var now = new Date().getTime();
var unix_date = user_date;
// check validity of date
if(unix_date == "") {
return "Bad date";
}
// is it future date or past date
if(now > unix_date) {
var difference = now - unix_date;
var tense = "ago";
} else {
var difference = unix_date - now;
var tense = "from now";
}
difference = difference/1000; //from milliseconds to seconds;
for(var j = 0; difference >= lengths[j] && j < lengths.length-1; j++) {
difference /= lengths[j];
}
difference = Math.round(difference);
if(difference != 1) {
periods[j] += "s";
}
return difference+" "+periods[j]+" "+tense;
}
console.log(nicetime(1378632182334));
例:
注意我在您的功能中所做的唯一更改是:
difference = difference/1000; //from milliseconds to seconds;
您的代码处理的差异变量以毫秒为单位,应以秒为单位。
@ra_htial在他的第一条评论中提出的建议听起来像是最好的建议,因为它不会不必要地轮询请求,而请求可以使用javascript在客户端进行管理。 因此,我的建议是向.date
span元素添加data-date
属性,该属性应包含纯格式的日期和时间,可以使用javascript进行解码并用于更新人类可读的日期。
这是带有静态数据的html的外观:
<span class="date" data-date="2013-09-08 12:37">1 min ago</span>
或从您的php脚本输出:
<span class="date" data-date="<?php echo $date; ?>"><?php echo nicetime($date); ?></span>
这是更新日期内容的javascript(我使用了本文http://www.zachleat.com/web/yet-another-pretty-date-javascript/中的格式化功能,但还有其他替代方法,例如John Resig的Pretty Date ):
setInterval(function()
{
$('.date').each(function()
{
var $this = $(this);
$this.html(humane_date($this.data('date')));
});
}, 10000);
编辑
这是一个小提琴,说明了功能http://jsfiddle.net/FXERS/ 。 您需要将小提琴中的元素之一的data-date
属性更改为当前时间,以查看其更新。
看起来您可以在多个元素上使用.load()
方法,但是您可以一次将日期提取到变量中,然后更新所有其他元素:
$.get('/comdate.php?id=' + id, function(data) {
$('.date').each( function() { $(this).html(data); } );
});
哪个更新所有元素。 尽管上面的注释中已指出,但是您在该脚本中并没有做任何工作,而这在JS中是无法完成的。
经过编辑后,在JavaScript中添加了 Look at at Date对象,它可以获取当前时间并进行解析-函数中的其他所有内容都只是易于翻译的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.