1

I'm doing ajax request(inside setInterval) inside another ajax , but I have to abort inner ajax after every tick and send it again every interval of time. Why I need setInterval? Because I have to check for an answer from server every second(user should call to number to activate feature). Why I need to abort request? Because while when I'm sending requests to server I can't go to another page/update page/ click to another another button to generate new number where to call(Actually I can, but if ajax isn't aborted it will wait until timeout), but it should be possible.

Question: How to give an opportunity to leave page, click to another button, maybe do abort(but on new time interval continue send requests)? enter image description here On the image you can see that I got console log 20 times and ajax haven't been sent. (aborted at the end)

Looking for your advices, feel free to ask enything you need for better understanding

jQuery:

var ctimer;
var reply_wait;
var mpay_url = php.ajaxurl;
var ajax_reply;
var ajax_reply2;

/* Payment Start Listener */
jQuery(".mobile_pay_action").click(function () {

    clearInterval(ctimer);
    clearInterval(reply_wait);

    var m_pay_container = jQuery(this).data('container');
    var m_pay_case = jQuery(this).data('articleId');
    var static_id = jQuery(this).data('staticId');

    jQuery(m_pay_container).show();

    var pay = jQuery.ajax({
        type: "POST",
        url: mpay_url,
        data: {
            artid: m_pay_case,
            action: 'mobile_pay_ajax',
            action2: 'pay'
        },
        success: function (r) {
            var d = JSON.parse(r);
            // console.log(d);
            jQuery('#mobile_pay').html(d.msg);
            if (d.status === "pay_wait") {

                ctimer = setInterval(function () {
                    m_pay_countdown();
                }, 1000);


                reply_wait = setInterval(function () {


                    ajax_reply = jQuery.ajax({
                        url: mpay_url,
                        data: {
                            'action2': 'pay_reply',
                            action: 'mobile_pay_ajax',
                            'artid': m_pay_case,
                            'static_article_id': static_id,
                            'm_pay_div': m_pay_container
                        },
                        dataType: "json",
                        success: function (data) {

                            if (typeof data.status !== 'undefined') {
                                if (data.status === "done") {
                                    clearInterval(ctimer);
                                    clearInterval(reply_wait);

                                    var expires = new Date();

                                    if(m_pay_case > 0 ){
                                        jQuery.cookie("newspaper_article_access_" + m_pay_case, m_pay_case, {
                                            expires : 30,
                                            path    : '/'
                                        });
                                    }else{
                                        expires.setTime(expires.getTime() + (60 * 60 * 1000 * 24));
                                        jQuery.cookie("newspaper_one_day_access", 'access', {
                                            expires : expires,
                                            path    : '/'
                                        });
                                    }

                                    jQuery('.article_content_locker').hide();
                                    jQuery('.article.full_article .article_content').html(data.content);

                                }
                            }
                        }

                    });


                    ajax_reply.abort();

                    console.log('request');

                }, 1000);

            }
        },
        error: function (e) {
            alert('Error: ' + e);
        }
    });

    return false;
});

function m_pay_countdown() {
    var sec = parseInt(jQuery('#m_pay_counter').text()) || 0;
    jQuery('#m_pay_counter').text(--sec);
    if (sec == 0) {
        var text = '<div class="mobile_payment_content_box"><span class="time_out">Aeg sai otsa!</span></div>';
        jQuery('#mobile_pay').html(text);
        clearInterval(ctimer);
        clearInterval(reply_wait);
    }
}
Yannick Huber
  • 607
  • 2
  • 16
  • 35
vladys.bo
  • 730
  • 2
  • 11
  • 34

0 Answers0