如果当前页面加载x秒,则重定向到另一个页面?

只是好奇这是否可行。 我正在使用API​​,功能有时可能需要很长时间才能完成,甚至可能会超时,具体取决于它的感觉。 是否有办法(使用PHP,Javascript等)能够在X秒后停止加载页面并重新指向另一页?</ p>

具体来说,我' m使用使用标准表单发布机制发布到API的表单。</ p>
</ div>

展开原文

原文

Just curious if this could be possible. I am using an API and a function can sometimes take quite awhile to complete or even times out depending how it feels. Is there a way (Either with PHP, Javascript etc.) to be able to stop the page from loading after X amount of seconds and re-direct to another page?

Specifically, I'm using a form which is posted to the API using the standard form post mechanism.

doufu9521
doufu9521 是。
3 年多之前 回复
doucang8303
doucang8303 嗨,这是在完成一个表单并单击提交按钮后。然后将其发布到API。
3 年多之前 回复

1个回答

Depending on what it is that delays the slow page's response, the browser may not tear down the previous page for some time after the form submission. That means you have the opportunity to use a setTimeout to do the redirect.

document.querySelector("selector-for-the-form").addEventListener("submit", function() {
    setTimeout(function() {
        location.href = "/path/to/redirect/to";
    }, 3000); // 3000 = three seconds
}, false);

That works for me on Chrome, Firefox, and IE when posting to this simple PHP page in my default setup, which does a five-second busy-wait:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Delayed</title>
<style>
body {
    font-family: sans-serif;
}
</style>
</head>
<body>
<?php
$end = time() + 5;
while (time() < $end) {
    // wait
}
?>
<p>Done: <?php echo $_POST["foo"]?></p>
</body>
</html>

Again, though, it depends on whether it takes several seconds for the API page to start sending data to the browser. If it's not caching, for instance, and starts sending a response right away but then takes a long time to finish sending its response, the browser may tear down the page and start building the new one.

douhao6271
douhao6271 工作完美。 谢谢!
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐