函数后JS位置加载 - 添加延迟时间

JS location load after function - Add delay time

我做了一个 function.php 并在处理函数后包含了一个重定向(到另一个页面)。

效果很好 - 我点击/函数执行并重定向。

但是

我不希望它立即重定向并延迟几秒钟,因为我有一条消息"Succesuful added.. "

最后一部分包含 JS:

//Display ID
    echo "<div align='center'> <h4>*Command completed! NR: ".$id_receptie. </h4></div>";
     echo "<script type='text/javascript'>
window.onload = function () { top.location.href = 'fisa_service.php?id=" . $id_receptie . "'; };
</script>";

试试 setTimeout 函数。

setTimeout(function(){
  // redirect
}, 2000);

2000 是以毫秒为单位的秒数。

希望对您有所帮助。

您可以在 PHP 中使用 sleep() 函数:http://php.net/manual/fr/function.sleep.php

尝试下一个脚本:

<?php

$timeout = 2;   // Timeout in the seconds
$id_receptie = 0;

?>
<div align='center'><h4>*Command completed! NR: <?= $id_receptie ?> </h4></div>
<script type='text/javascript'>
    setTimeout(function() {
        top.location.href = 'fisa_service.php?id=<?= $id_receptie ?>';
    }, <?= $timeout ?>*1000);
</script>
<?php

exit;