使用jQuery加载函数比较字符串

时间:2017-08-22 01:00:36

标签: javascript jquery comparison

编写代码我遇到了一件事。我通过jQuery加载函数加载变量字符串,并且有麻烦开始。我希望我的代码检查通过文本文件加载的字符串是否有任何更改,如果这是真的,请执行一些操作。如何将变量设置为字符串以将其与文件中的变量进行比较?部分代码

var follow, donate;
var auto_refresh = setInterval(function() {

follow = $('#followerid').load("../Muxy/most_recent_follower.txt");
donate = $('#donatorid').load("../Muxy/most_recent_donator.txt");

}, 100);

然后我想做这样的事情:

someUpdateFunction() {

if($(#'followerid').get("innerHTML") != follow)
// actions (animations, div changes etc.)

}

这可能完全错了,但我在这里找不到任何提示。提前致谢

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$.get('../Muxy/most_recent_follower.txt', function (data) {
    var followerid = $('#followerid').html();
    if( followerid == data ){
        // They are the same
    }else{
        // They are not the same
    }
});

.html()方法将获取#followerid内的HTML。这听起来像是你想要的。

答案 1 :(得分:0)

jQuery.load函数不返回加载的文件,它返回元素。试试这个:

var auto_refresh = setInterval(function() {

   oldFollow = $('#followerid').html();

   $('#followerid').load("../Muxy/most_recent_follower.txt");

   oldDonate = $('#donatorid').html();

   $('#donatorid').load("../Muxy/most_recent_donator.txt");

}, 100);

someUpdateFunction() {
    if($('#followerid').html() !== oldFollow)
    // actions (animations, div changes etc.)

    }
}