简体   繁体   English

多次调用javascript函数

[英]Call javascript function multiple times

So, I'm having some troubles finding a way to solve this problem. 因此,我在寻找解决此问题的方法时遇到了一些麻烦。 I know that it seems silly, but I'm really stuck on this. 我知道这似乎很愚蠢,但我确实对此感到困惑。

I have something like: 我有类似的东西:

<div class="tdate">
    Sat Oct 11 01:11:01 +0000 2014
</div>
<div class="tdate">
    Sat Oct 11 01:10:44 +0000 2014
</div>
<div class="tdate">
    Sat Oct 11 00:51:03 +0000 2014
</div>

And this javascript function: 而这个JavaScript函数:

function parseTwitterDate(tdate) {
    var system_date = new Date(Date.parse(tdate));
    var user_date = new Date();
    if (K.ie) {
        system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
    }
    var diff = Math.floor((user_date - system_date) / 1000);
    if (diff <= 1) {return "just now";}
    if (diff < 20) {return diff + " seconds ago";}
    if (diff < 40) {return "half a minute ago";}
    if (diff < 60) {return "less than a minute ago";}
    if (diff <= 90) {return "one minute ago";}
    if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
    if (diff <= 5400) {return "1 hour ago";}
    if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
    if (diff <= 129600) {return "1 day ago";}
    if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
    if (diff <= 777600) {return "1 week ago";}
    return "on " + system_date;
}
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();

What I'm trying to do is to change all the .tdate to a new format (which is what my javascript does). 我想做的是将所有.tdate更改为新格式(这是我的JavaScript所做的)。 But I dont know how to call this function to change all the different .tdate. 但是我不知道如何调用此函数来更改所有不同的.tdate。 If I call parseTwitterDate("Sat Oct 11 00:51:03 +0000 2014"), it will work, but only one time. 如果我调用parseTwitterDate(“ Sat Oct 11 00:51:03 +0000 2014”),它将起作用,但是只有一次。

Basically, I want to see all the date on my page with this format: "Sat Oct 11 01:11:01 +0000 2014" change to: "47 minutes ago" (or whatever the output is). 基本上,我想以以下格式在页面上查看所有日期:“ Sat Oct 11 01:11:01 +0000 2014”更改为:“ 47分钟前”(或任何输出)。 But I dont know how to call the function parseTwitterDate(tdate). 但是我不知道如何调用函数parseTwitterDate(tdate)。

Sorry for my really bad explanation. 对不起,我的解释很不好。 Let me know if I don't make myself clear enough. 让我知道我是否不够清楚。

And thank you very much to anyone who can help me. 非常感谢任何可以帮助我的人。

Thanks a lot! 非常感谢! :) :)

Since you are using JQuery (or at least the JQuery tag is used), you can do this 由于您正在使用JQuery(或至少使用了JQuery标记),因此可以执行此操作

 $(function () { $('.tdate').each(function (index, value) { $(value).html(parseTwitterDate(($(value).html()))) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tdate"> Sat Oct 11 01:11:01 +0000 2014 </div> <div class="tdate"> Sat Oct 11 01:10:44 +0000 2014 </div> <div class="tdate"> Sat Oct 11 00:51:03 +0000 2014 </div> 

Wadup! Wadup! add this in your javascript file. 将此添加到您的javascript文件中。

...

[].forEach.call(document.getElementsByClassName("tdate"), function(tdate) {
  tdate.innerHTML = parseTwitterDate(tdate.innerHTML);
});
$('.tdate').each(function() {
    $(this).text(parseTwitterDate($(this).text()));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM