How do i use the await event in php echo

peter-cooper
peter-cooper used Ask the Experts™
on
Hello
I have a script that echoes to a location at the end of a mysql query. I need to set a time before the function executes and found that the javascript event is 'await. I have tried in my code, but all I get is 500 error. Can someone please help with this. Thanks

echo "<script language=\"JavaScript\">\n"; 
echo 'alert("SUCCESS. You will now be redirected to the boxes requests area.");'; 
echo 'await sleep(4000)';
echo 'location.href = "boxdest.php"'; 
echo "</script>";

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Full Stack Developer
Commented:
For this in JavaScript we have setTimeout function:

echo "<script language=\"JavaScript\">\n"; 
echo 'alert("SUCCESS. You will now be redirected to the boxes requests area.");'; 
echo 'setTimeout(function(){ location.href = "boxdest.php" }, 4000)';
echo "</script>";

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
I don't think Await does not do what you think it does.
Firstly this is still in draft - part of ES2017 - so it is not guaranteed to be available
Secondly it is supposed to be used in async functions waiting the resolution of a promise.

What you should be using is setTimetout

setTimeout(function() {
   location.href = "boxdest.php";
}, 4000)

Open in new window


Or alternatively outputing a meta-refresh tag in the header
<meta http-equiv="refresh" content="4;URL='boxdest.php'" />

Open in new window

Author

Commented:
Thanks for the help
Mukesh YadavFull Stack Developer

Commented:
You are welcome! :)
Most Valuable Expert 2011
Top Expert 2016

Commented:
The corresponding functions in PHP are
http://php.net/manual/en/function.sleep.php - for long delays
http://php.net/manual/en/function.usleep.php - for short delays

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial