0

Hi im trying to run a AJAX loop, to update the page every x seconds this is the code

function ready() {
    var form = $('#formcomments');
    var submit = $('#comments');

    // send Ajax request
    $.ajax({
        url: <?= json_encode($this->make_base_url("user/comments_all/{$resdata['id']}/")) ?>,
        type: 'POST',
        data: form.serialize(), //form serizlize data
        beforeSend: function() {
            // change submit button value text and disabled it
            submit.val('Enviando...').prop('disabled', true);
        },
        success: function(data) {
            // Append with fadeIn see http://stackoverflow.com/a/978731
            var item = $(data).hide().fadeIn(800);
            $('.module').append(item);
            var objDiv = document.getElementById("modulecomm");
            objDiv.scrollTop = objDiv.scrollHeight;
            // reset form and button
            form.trigger('reset');
            submit.val('Submit Comment').prop('disabled', false);
        },
        error: function(e) {
            alert(e);
        }
    });
}

And this is the time loop

window.setInterval(function(){
  ready();
}, 55000);

After lets say 10 minuts i get an alert with an error object[Object]

how can i get rid of this error.

Moncho Chavez
  • 694
  • 2
  • 10
  • 31

1 Answers1

1

You can remove the error by deleting:

    error: function(e) {
        alert(e);
    }

from your code.

However, I imagine you want to make your ajax call work and you probably need a reasonable url in order for that to happen. Whatever you're doing with url: looks crazy. You can easily debug that by typing a normal path directly in there and seeing if it works. Also, you may be 'POST'ing to a forbidden url or something to that effect. The console log is your friend here.

Further, your ajax call is set to run ever 55 seconds. You say that you get an error after about 10 minutes. If that is true, then your ajax call must be working fine most of the time, but after several executions, something breaks. That probably means something your code is eventually creating a state that it can't properly handle. You would need to track that down and fix it. That's beyond the scope of a stackoverflow question. Basic debugging skills are a must have for any developer.

m59
  • 43,214
  • 14
  • 119
  • 136
  • Thanks, actually in the past i have a normal url and for other reasons some one recomend me to do a json overthere.. what i need is not to post but to get that page and insert it there , and update that every period of time... What would you do?. I use post to send id for example. – Moncho Chavez Nov 16 '13 at 05:30
  • @MonchoChavez I have no idea what you just said. You don't need to take other people's advice and start putting in code if you don't know what it does. I don't know what you're trying to accomplish. If you have a specific question about how to do something, post another question asking about that. – m59 Nov 16 '13 at 05:43