简体   繁体   中英

JS location load after function - Add delay time

I have made a function.php and I included a redirect (to another page) after processing the function.

It works great - I click / function executes and it redirects.

BUT

I want it not to instant redirect and to delay a couple of seconds , since I have a message "Succesuful added.. "

Last part it includes the 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>";

Try the setTimeout function.

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

The 2000 are the seconds in milliseconds.

Hope this helps.

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

Try next script:

<?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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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