页面重定向在 PHP 或 JavaScript 中不一致

Page redirect not working consistently in PHP or JavaScript

我在这里阅读了各种资源,并创建了以下在 10 秒后重定向用户的方法。未同时受雇

PHP 中的首次尝试:

header("refresh:10;url=?switch=1")

然后,JavaScript中的第二次尝试:

window.setTimeout(function () {
    location.href = '?switch=1';
}, 10000);

他们都几乎一直工作。它们在使用 reveal.js (like this one) 的页面上使用。有时,当 URL 片段发生变化时,页面根本不再重定向 - 尽管通常情况下,重定向确实有效。

任何人都可以让我知道可能是什么问题,或者可以阅读有关该问题的好资源吗?

编辑:在 JavaScript 版本中更改为 setInterval 会导致再次尝试。

每十秒从一个问题转到另一个问题的quiz/memory游戏?

与其每隔 x 秒重新加载一次应用程序,不如构建一个 one-page 应用程序,从您的 PHP 脚本中提取数据,然后每隔 x 秒显示一次。

一个非常简单的例子 (non-tested):

<!doctype html>
<html>
<head>
    <title>Example</title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>

Question #<span id="question_number"></span><br />
<p id="question"></p>

<script>
var questionCounter = 0;

function loadQuestion(questionNumber) {
        questionCounter++;

        $.get( "<your_php_script_url>?question="+questionCounter, function( data ) {
                $( "#question_number" ).html( questionCounter );
                $( "#question" ).html( data.question );

                setTimeout(function() {
                    loadQuestion(questionCounter);
                }, 10000);
        });
}
loadQuestion(questionCounter);

</script>

</body>
</html>

当使用 php 设置 headers 时,您必须在生成任何输出之前执行此操作,在某些情况下,您可能会在 php header 已设置。

"It is important to note that headers are actually sent when the first byte is output to the browser. If you are replacing headers in your scripts, this means that the placement of echo/print statements and output buffers may actually impact which headers are sent." - 来自 http://php.net/manual/en/function.header.php 你可能会发现该页面对阅读很有用(如果你还没有读过),我发现它在遇到 header 时很有用相关问题。

如果您想要更多 in-depth 答案,我会为您的问题添加更多上下文(和相关代码)。我们无法知道我们看不到的代码出了什么问题。如果您包括使用您正在设置的 URL 片段的代码,我们可能会更好地了解您的代码实际在做什么,并且可能能够捕获您遗漏的错误。这是关于片段的维基。只是为了完整性。 https://en.wikipedia.org/wiki/Fragment_identifier

最后,JavaScript 这个例子让我不知所措。它在我的系统上运行良好。 (尽管重新加载可能会延迟几微秒)。您可以尝试改用 setInterval() 吗?

    <!doctype html>
    <html>
    <head>
    <title>Example</title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    </head>
    <body>

    Question #<span id="question_number"></span><br />
    <p id="question"></p>

    <script>
    var questionCounter = 0;

    function loadQuestion(questionNumber) {
        questionCounter++;

        $.get( "<your_php_script_url>?question="+questionCounter, function( data            ) {
                $( "#question_number" ).html( questionCounter );
                $( "#question" ).html( data.question );

                setTimeout(function() {
                    loadQuestion(questionCounter);
                }, 10000);
        });
}
loadQuestion(questionCounter);

</script>

</body>
</html>

这是一个很好的例子,我用过这段代码。

Try this so simple example

setTimeout(function() {
    window.location='?switch=1'
}, 10000);

使用这个jquery代码

window.setTimeout(function (){
window.location.replace("?name=Munish");
},10000);