Jon_the_eye
Jon_the_eye

Reputation: 39

Repeat an AJAX call Until JSON response shows process complete

I am trying to convert a middleware (PHP type) page which bulk sends emails into a jQuery/Ajax Page, sending one email at a time. Which is invoked by a calling ajax function. I want to synchronously repeat the ajax call until the json response eg data.status = "finished", each call will return data.percentage, (percentage complete) to progressively update a simple progress bar. As well as this I would like to include a "stop" button to cancel the broadcast. The calling page won't know how many calls it needs to make. The middleware will generate the next email and talk directly to the SMTP host and mark that email as sent, work out how far through it is so it can send back the percentage done.

I have seen some variations of this, but most solutions seem to know how manu times to loop the ajax call.

Any code snippets appreciated. Here is my code without any loop or stop code.

jQuery("#sendButton").click(function() {
   var ezineid = jQuery("#ezineid").val() ;
   var percentage = 0 ;
   var status = "" ;
   jQuery.ajax({  url: '/cms/emailsend.lasso',
       type: 'POST',
       dataType: 'json',
       async: false,
       data: {
       ezineid: ezineid
       },
       success: function(data) {
           // have the html to display now
           percentage = data.percentage ;
           status = data.status ;
           // update the progress bar  
           // data.status = finished?
         }
   });
});

Upvotes: 0

Views: 329

Answers (1)

Stevan Tosic
Stevan Tosic

Reputation: 7209

var timeout = setTimeout(function () {
         $.ajax({
             url: "test.html",
             context: document.body
         }).done(function() {
          clearTimeout(timeout)
});
    }, 100);

You can try this, but you will need node.js and sockets if you want to listen for an event to happen.

Upvotes: 0

Related Questions