简体   繁体   中英

WordPress ajax call is being called recursively

I am using this function to run an ajax code

add_action('wp_ajax_my_action', array(&$this,'ChatAjax'));

This code should be called each 30 sec, I am using this code

function CheckRequests(){
    jQuery.ajax({ 
    url : Chat.ajaxurl,
    type:'POST',
    data: 'action=my_action',
    success: function(data){jQuery('#LiveChat').html(data);}  
    });
    window.setInterval(function(){CheckRequests()}, 30000);
}

It works more than fine except, the function is not called every 30 sec. The code is called each second, again and again, so the whole web site was suspended by my hosting provider.

That is because the code for setting up the callback is inside the function scope, and this is causing recursive call.

function CheckRequests(){
    jQuery.ajax({ 
        url : Chat.ajaxurl,
        type:'POST',
        data: 'action=my_action',
        success: function(data){jQuery('#LiveChat').html(data);}  
    });
}//close the bracket here
//call should be outside the function scope 
window.setInterval(CheckRequests, 30000);

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