clearInterval() not working in ajax in php with both get and post methods called on submit

606 views Asked by At

I am using setInterval and then unable to clearInterval in my code. Please see the code I am getting the value of complete and it is true and also entering the if statement but clearInterval still not working. I am tired and trying for last 10 hours but not find the bug. I checking the console.log and it is not stopping the interval.

jQuery(document).ready(function() {
    var complete = false;
    var Interval;

    jQuery( "#form" ).submit(function() {
        Interval = setInterval(get, 100);
        post();
        return false;
    });

    function post(){

        var url = "my/url/here";

        var data = {
        'action': 'action/here',
        'name': 'value'
        };
        jQuery.post(url, data, function(response) {
            jQuery("#emails").html(response);
        }).done(function() {
        //alert( "second success" );
        })
        .fail(function() {
        //alert( "error" );
        })
        .always(function() {
         complete = true;   
        //alert( "finished" );
        });

    } // End post()
    function get(){

        var url = "url/goes/here";

        if(complete == true){
            clearInterval(Interval);
        }
        var data = {
            'action': 'action/here',
        };

        jQuery.get(url, data, function(response) {
            console.log(response);
            jQuery("#progress").html(response);     
        });

    } // End get()

});

here is the original code below. ajax call is working fine but clearInterval() not working

<script type="text/javascript" >
jQuery(document).ready(function() {

    jQuery(".juee_emails_row").hide();

    var juee_complete = false;
    var juee_Interval;

    jQuery( "#juee_form" ).submit(function() {
        jQuery(".juee_emails_row").show();
        juee_Interval = setInterval(juee_get, 100);
        juee_post();
        return false;
    });

    function juee_post(){
        var data = {
        'action': 'juee_get_emails',
        'juee_data_option': jQuery('input[name=juee_data_option]:checked', '#juee_form').val()
        };
        jQuery.post(ajaxurl, data, function(response) {
        jQuery("#juee_emails_td").html(response);
        }).done(function() {
        //alert( "second success" );
        })
        .fail(function() {
        //alert( "error" );
        })
        .always(function() {
         juee_complete = true;  
        //alert( "finished" );
        });

    } // End juee_post()
    function juee_get(){
        if(juee_complete == true){
            clearInterval(juee_Interval);
        }
        var data = {
            'action': 'juee_progress',
        };

        jQuery.get(ajaxurl, data, function(response) {
        console.log(response + " email found");
        jQuery("#juee_progress_td").html("");
        jQuery("#juee_progress_td").html(response);     
    });

    } // End juee_get()

});
</script> 
0

There are 0 answers