Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在WordPress中使用jQuery定期调用Ajax?_Javascript_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript 如何在WordPress中使用jQuery定期调用Ajax?

Javascript 如何在WordPress中使用jQuery定期调用Ajax?,javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,我想每30秒更新WordPress小部件中的信息。因此我认为我最好使用一些Ajax 我在上找到了它,并将其添加到my functions.php: add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here function my_action_javascript() { ?> <script type="text/javascript" > jQuery(document

我想每30秒更新WordPress小部件中的信息。因此我认为我最好使用一些Ajax

我在上找到了它,并将其添加到my functions.php:

add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here

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

    var data = {
               'action': 'my_action',
               'whatever': 1234
    };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
    });
});
</script> <?php
}

add_action( 'wp_ajax_my_action', 'my_action_callback' );

    function my_action_callback() {
    global $wpdb; // this is how you get access to the database
    $bla = get_option("airtime_now_playing");
    echo $bla;
    wp_die(); // this is required to terminate immediately and return a proper response
}
它工作正常,确实能找回我需要的东西

我怎样才能不只是一次而是每30秒发射一次

非常感谢

您需要使用setInterval函数。 仅供参考:如果您只需要在30秒后发出一次ajax调用,则需要使用setTimeout,等待30秒,然后执行一次函数

setInterval(function(){
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
    });
}, 30000);
您需要使用setInterval函数。 仅供参考:如果您只需要在30秒后发出一次ajax调用,则需要使用setTimeout,等待30秒,然后执行一次函数

setInterval(function(){
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
    });
}, 30000);

看看这个谢谢。是的……这和我遇到的问题是一样的,在那里解决了。看看这个谢谢。是的……这和我遇到的问题是一样的,并且在那里得到了解决。