JS位置加载后的功能-添加延迟时间


JS location load after function - Add delay time

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

它工作得很好-我点击/function执行,它重定向。

我希望它不立即重定向,并延迟几秒钟,因为我有一个消息"成功添加…"

最后一部分包括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;