简体   繁体   English

重复AJAX调用,直到JSON响应显示过程完成为止

[英]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. 我正在尝试转换一个中间件(PHP类型)页面,该页面将大量电子邮件发送到jQuery / Ajax页面中,一次发送一封电子邮件。 Which is invoked by a calling ajax function. 通过调用ajax函数来调用。 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. 我想同步重复ajax调用,直到json响应,例如data.status =“ finished”,每个调用将返回data.percentage(完成百分比)以逐步更新一个简单的进度栏。 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. 中间件将生成下一封电子邮件,并直接与SMTP主机对话,并将该电子邮件标记为已发送,计算出发送的距离,以便可以将完成的百分比发回。

I have seen some variations of this, but most solutions seem to know how manu times to loop the ajax call. 我已经看到了一些变化,但是大多数解决方案似乎都知道循环ajax调用的操作时间。

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. 您可以尝试一下,但是如果您想监听事件的发生,则需要使用node.js和套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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