-4

I would like to compare data to determine if the div needs to be reloaded.

    // <![CDATA[
$(function () {

    function reload (elem, interval) {
        var $elem = $(elem);
        var $original = $elem.html();


        $.ajax({
            cache : false,
            url : '/inbox-header.php',
            type : 'get',
            success : function (data) {

                var result = $.trim(data);
                var resu = $.trim($original);


                console.log(result);

                if (result == resu) {

                    alert('a');

                    setTimeout(function () {
                        reload(elem, interval)
                    }, interval);
                    return;
                }

                $elem.html(data);

                setTimeout(function () {
                    reload(elem, interval)
                }, interval);
            }
        });
    }

    reload('#inboxheader', 500);
});
// ]]>

When I show the output in the console it looks the same, but the alert never shows, so its always false.

UPDATE:

The output of those variables can be found here, unable to post them here..

http://pastebin.com/abfCk7pH

Michael
  • 31
  • 2
  • Use `console.log` => `console.log("result = " + $.trim(data) + "resu = " + $.trim($original))` and see what you see? – Praveen Kumar Purushothaman Jun 14 '16 at 11:22
  • Using developer tools (typically F12 on many browsers) put a breakpoint here -> `if (result == resu)` and hover to find out what do you compare. Also have a look at http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – Mark Jun 14 '16 at 11:23
  • Also provide your HTML in your question, since that could provide some insight for answering the question. – Arathi Sreekumar Jun 14 '16 at 11:26
  • the response of the html is : http://pastebin.com/abfCk7pH – Michael Jun 14 '16 at 11:33

1 Answers1

0

I dont know why but the trim function did not do his job.

this works:

$(function() {
function reload(elem, interval) {
    var $elem = $(elem);
    var $original = $elem.html();
    $.ajax({
        cache: false,
        url: '/inbox-header.php',
        type: 'get',
        success: function(data) {
            var opgehaaldedata = data.replace(
                /(\r\n|\n|\r)/gm, "");
            var orgineledata = $original.replace(
                /(\r\n|\n|\r)/gm, "");
            if (opgehaaldedata == orgineledata) {
                //alert('a');
                setTimeout(function() {
                    reload(elem, interval)
                }, interval);
                return;
            } else {
                $elem.html(opgehaaldedata);
                setTimeout(function() {
                    reload(elem, interval)
                }, interval);
                return;
            }
        }
    });
}
reload('#inboxheader', 500);

});

Michael
  • 31
  • 2