How to make response from php to ajax to finish running the php code?

Good evening!
I'm making a plugin for wp. The plugin adds a form via shortcode. After they submit the form via ajax I write the data in the database and I need to send a response that the data was successfully written to the echo example 1; or not successful then echo 0; then js will display an appropriate message to the user.
The main thing is that after this answer you need to run other php code with a delay. For example, send mail to
after 10 minutes, I write a sleep(600); $mail->send(); But then the answer comes via ajax is also only 10 min.
I tried to use the function ob_flush(); flush(); after echo on a separate php page output occurs before the end of the execution of php code, but in my plugin there is no reply.
March 23rd 20 at 19:41
4 answers
March 23rd 20 at 19:43
Solution
Use tasks (CZK), for example, insert a table of tasks and a time stamp when the task should run, then the issue will be resolved
March 23rd 20 at 19:45
Solution
If you have a plugin that prevents the use of crowns?
1. Write the data in the table, column flag default is zero.
2. Kron is run every 10 minutes for a table looking for rows with flag zero, collect these rows in an array, the flag is set edinichku, the array give the mailer script.
March 23rd 20 at 19:47
You can't force the server to sleep. This is a terrible waste of resources. The first and main task of the server is "as soon as possible to give the answer and be ready to receive next request". Moreover, the hoster will not allow you to run the script 10min. By default, the script is given 30 seconds.
What's stopping You to send mail, and then return the answer? If You want to give the answer, and then send mail, then look in the direction of the cron task.
March 23rd 20 at 19:49
There are asynchronous php, but I never managed to make it work. Generally this is from the evil one. Can you do two ajax request? One on the record in the database and another to send emails? And anyway, I'd do it all at once. Would be recorded in database recorded in another table, that should be sent after 10 minutes the letter and give response to user. A cron job runs every 10 minutes would be sent a letter.

Find more questions by tags WordPressPHPAJAX