简体   繁体   English

WordPress Ajax调用被递归调用

[英]WordPress ajax call is being called recursively

I am using this function to run an ajax code 我正在使用此功能运行Ajax代码

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

This code should be called each 30 sec, I am using this code 此代码应每30秒调用一次,我正在使用此代码

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. 除了没有每30秒调用一次该函数外,它的工作原理还不错。 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);

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

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