Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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 制作自动更新、facebook风格的时间格式_Javascript_Templates_Jquery Templates_Mustache - Fatal编程技术网

Javascript 制作自动更新、facebook风格的时间格式

Javascript 制作自动更新、facebook风格的时间格式,javascript,templates,jquery-templates,mustache,Javascript,Templates,Jquery Templates,Mustache,有人能帮我弄明白为什么我的javascript HTML模板不能自动更新时间,而常规HTML却能自动更新时间吗?以下是我正在使用的模板库: 这是我的JS(你可以在这里摆弄它:): $(document.body).on('click','button',function()){ var id=$(this.data('id'); var data={id:id,string:“刚才…”,fxn:nicetime()}; var result=tmpl(“{%=o.string%}{%=o.fxn

有人能帮我弄明白为什么我的javascript HTML模板不能自动更新时间,而常规HTML却能自动更新时间吗?以下是我正在使用的模板库:

这是我的JS(你可以在这里摆弄它:):

$(document.body).on('click','button',function()){
var id=$(this.data('id');
var data={id:id,string:“刚才…”,fxn:nicetime()};
var result=tmpl(“{%=o.string%}{%=o.fxn%}”,数据);
$('div[data id=“”+id+“]”)html(结果);
时间();
});
函数时间(){
变量时间=新日期(),
var comment_date=setInterval(函数(){
var time2=自(time.getTime()/1000)起的时间;
$('#time_-since').html(time2);
返回时间2;
}, 
1000);
}
HTML:

<button data-id="1">1</button>
<div data-id="1"></div>  //tmpl output
<div id="time_since"></div>  //non-tmpl output
1
//tmpl输出
//非tmpl输出
您想要类似的东西

使用JavaScript模板,您通常希望将模板化一次,然后动态更新特定元素的值,而不是每秒重置整个元素的innerHTML

下面是JavaScript:

$(document.body).on('click', 'button', function(){
    var id= $(this).val(),
        time = +new Date,
        data = {
            id: id,
            string: "just now..."
        },    
        result = tmpl('<span class="string">{%=o.string%}</span>', data),
        tgt = $('#'+id),
        str;
    tgt.html(result);    
    str = tgt.find('.string');
    window.setInterval(function() {
        str.html(time_since(time/1000));
    }, 1000);
});
$(document.body).on('click','button',function()){
var id=$(this.val(),
时间=+新日期,
数据={
id:id,
字符串:“刚才……”
},    
结果=tmpl('{%=o.string%}',数据),
tgt=$(“#”+id),
str;
html(结果);
str=tgt.find('.string');
setInterval(函数(){
str.html(time_-since(time/1000));
}, 1000);
});

Jquery data()函数与数据属性无关,只需使用attr()。@Musa谢谢,你对setInterval问题有什么建议吗?太棒了,让我仔细研究一下,但我认为这会奏效!好的,现在我明白了。将模板值保留为字符串,然后在放置模板后查找并对这些值运行setInterval。谢谢
$(document.body).on('click', 'button', function(){
    var id= $(this).val(),
        time = +new Date,
        data = {
            id: id,
            string: "just now..."
        },    
        result = tmpl('<span class="string">{%=o.string%}</span>', data),
        tgt = $('#'+id),
        str;
    tgt.html(result);    
    str = tgt.find('.string');
    window.setInterval(function() {
        str.html(time_since(time/1000));
    }, 1000);
});