简体   繁体   中英

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?
         }
   });
});
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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