简体   繁体   English

jQuery:加载另一个页面时不执行$ .ajax

[英]jQuery: $.ajax is not executed while another page is loading

I have a button that redirects to a page. 我有一个重定向到页面的按钮。 The PHP script of the page I want to redirect to takes some minutes. 我要重定向到的页面的PHP脚本需要花费几分钟。 While waiting for the PHP script I want a setInterval-scheduler to refresh content with jQ AJAX, but it doesn't seem to be executed while the redirection is loading. 在等待PHP脚本时,我希望setInterval-scheduler用jQ AJAX刷新内容,但是在加载重定向时似乎没有执行它。 Any ideas? 有任何想法吗?

Code: 码:

heartbeatInterval = 10000;
$(document).ready(
    function() {

        console.log('Document got ready.')

        setInterval(function() {
            heartbeat();
        }, heartbeatInterval);
});

$('#button').click(function() {
    window.location.href = '/...';
});

function heartbeat() {

console.log('Heartbeat triggers.');

$.ajax({
    url : action_heartbeat,
    type : 'POST',
    data : 'test',
    success : function(response) {
        $('#status').css('color', 'green').html($('#status').html() + '<br>Connected:  ' + response);
        console.log('Heartbeat finishes.');
    },
    error : function(xhr, message) {
        $('#status').css('color', 'red').html($('#status').html() + '<br>Connection lost: ' + message);
    }
});

}

This may not be possible. 这可能是不可能的。 Note that PHP (server side) runs before any javascript (client side). 请注意,PHP(服务器端)在任何javascript(客户端)之前运行。

So You would need to have the PHP finish first. 因此,您需要首先完成PHP。 There may be a way to run the PHP in an iframe embedded in the page, which might allow for faster loading of the page that has the javascript you want to run in it. 可能存在一种在页面内嵌的iframe中运行PHP的方法,这可能允许更快地加载要在其中运行javascript的页面。

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

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