更新页面时,心跳刷新浏览器



我试图在后端更新页面时在特定页面上自动强制浏览器刷新。我正在尝试使用心跳API来实现。

我浏览了我能找到的所有心跳示例,但我无法掌握它的整个概念。

我所能做的就是console.log在此特定页面上每15秒log一个字符串。

我到目前为止拥有的内容:

https://pastebin.com/elq9ujaw

<?PHP  
//embed heartbeat api
    function heartbeat_test_enqueue($hook_suffix) {
            if ( is_page(1105)) {
                // Make sure the JS part of the Heartbeat API is loaded.
                wp_enqueue_script('heartbeat');
                // Output the test JS in footer.
                add_action( 'print_footer_scripts', 'heartbeat_test_js', 20 );
                //Add filter to receive hook, and specify we need 2 parameters.
                add_filter( 'heartbeat_received', 'dw_receive_heartbeat', 10, 2 );
            }
    }
    add_action( 'wp_enqueue_scripts', 'heartbeat_test_enqueue' );

    //clientside
    function heartbeat_test_js() {
            ?>
            <script>
            jQuery(document).ready( function($) {
                    // Hook into the heartbeat-send
                    jQuery( document ).on( 'heartbeat-send', function( e, data ) {
                        //
                    });
                    // Listen for the custom event "heartbeat-tick" on $(document). This fire's once every minute that the page is open.
                    jQuery(document).on( 'heartbeat-tick', function(e, data) {
                        console.log("tick");
                    });
            });
            </script>
            <?php
    }

    //change heartbeat interval time
    function m_heartbeat_settings( $settings ) {
       $settings['interval'] = 15; //only values between 15 and 60 seconds allowed
       return $settings;
    }
    add_filter( 'heartbeat_settings', 'm_heartbeat_settings' );
    //heartbeat api end
?>

我想使用" post_updated"挂钩检查" post_date"是否已更改。但是我不明白如何将此钩与心跳API结合使用。

事先感谢,我真的在这里迷路了。

终于找到了一种方法。

//embed heartbeat api
function heartbeat_test_enqueue($hook_suffix) {
        if ( is_page(1105)) {
            // Make sure the JS part of the Heartbeat API is loaded.
            wp_enqueue_script('heartbeat');
            // Output the test JS in admin footer.
            add_action( 'print_footer_scripts', 'heartbeat_update', 20 ); 
            //Add filter to receive hook, and specify we need 2 parameters.
            add_filter( 'heartbeat_received', 'dw_receive_heartbeat', 10, 2 );
        }
}
add_action( 'wp_enqueue_scripts', 'heartbeat_test_enqueue' );
//write timestamp file
function write_timestamp($post_ID, $post_after, $post_before){
    if ( $post_ID == 1105 ) {
        $myfile = fopen("URLtoTextFile/tv.txt", "w") or die("Unable to open file!");
        $txt = $post_after->post_modified;
        fwrite($myfile, $txt);
        fclose($myfile);
    }
}
add_action( 'post_updated', 'write_timestamp', 10, 3 );
//clientside
function heartbeat_update() {
        ?>
        <script>
        jQuery(document).ready( function($) {
                var current_timestamp;
                jQuery.ajax({
                    url: "/tv.txt",
                    cache: false,
                    success: function (data){
                        current_timestamp = data;
                }});
                // Listen for the custom event "heartbeat-tick" on $(document). This fire's once every 15s that the page is open.
                jQuery(document).on( 'heartbeat-tick', function(e, data) {              
                    jQuery.ajax({
                        url: "/tv.txt",
                        cache: false,
                        success: function (data){
                            if (data != current_timestamp) {
                                jQuery('body').fadeOut(1000, function(){
                                    location.reload(true);
                                });
                            }
                        }
                    });
                });
        });
        </script>
        <?php
}

//change heartbeat interval time
function m_heartbeat_settings( $settings ) {
   $settings['interval'] = 15; //only values between 15 and 60 seconds allowed
   return $settings;
}
add_filter( 'heartbeat_settings', 'm_heartbeat_settings' );
//heartbeat api end 

我在做什么:

我在.txt文件中写下页面的修改日期,在每个更新之后,我都将挂钩挂钩并检查日期是否更改。然后我只是用jQuery刷新浏览器。

相关内容

  • 没有找到相关文章

最新更新