Answer the question
In order to leave comments, you need to log in
How to run a function on a php timer?
Hello!
I'm doing some voting for VK avatars. The bottom line is that 30 seconds are given, people vote and after 30 seconds it launches a function that does a certain action.
Real-time is implemented through PHP sockets
And for such a "server" timer, I came up with the following implementations:
1. There is a script on the client that sends a request to the socket server after the round is completed. Supposed cons - anyone can "shamanize the code" and start the round earlier, and also if there is no one on the site, nothing will work
2. while + sleep on the socket server. But it is not clear how to implement it in parallel with the main code
3. Implement through the difference in unix-time. That is, before the start of the round, the current time is saved, then after each request, the time difference is calculated before the start of the round and when the request is sent, if 30 or more seconds, we run the function. Cons - the round may not last exactly 30 seconds, but much longer
If this is a stupid question and ug-implementation, do not scold too much, I'm trying to gradually understand PHP
Well, the question is: how to implement a certain function every 30 seconds? (well, or every 30 seconds the same echo output)
Answer the question
In order to leave comments, you need to log in
// cron.
I don't agree, since cron itself can't run tasks more than once a minute - you can't do without a collective farm, and this is already too complicated for this task.
//Real-time is implemented via sockets in php
So you have a cli-script that runs from the console.
If sockets in php are implemented using Ratchet, then here is my answer to you at the link.
klats
Everything is detailed there - I'm sure it will help 100%
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question