Answer the question
In order to leave comments, you need to log in
How to competently organize a script call every N seconds?
There is a script whose task is to receive new messages on the VK page every N seconds using the getDialogs method.
There is no cron on my server, so I use external sites, one of them is cron-job.org. I call this script of mine once a minute.
set_time_limit(59);
require_once("start_func.php");
//ждём когда последний запущенный скрипт закончит работу
$time_shift = 0;
while(file_get_contents("cron.txt")!=0 & $time_shift<59)
{
sleep(1);
$time_shift++;
}
//говорим всем о том что запускаем скрипт
file_put_contents("cron.txt", 1);
$interval = 3;
$start_time = time();
//выполняем скрипт $i раз
while($time_shift<57 & time()-$start_time<57)
{
start_func(); //Делаем запрос к ВК АПИ
sleep($interval);
}
//говорим всем о том, что текущий скрипт закончил выполнение
file_put_contents("cron.txt", 0);
Answer the question
In order to leave comments, you need to log in
while(file_get_contents("cron.txt")!=0 & $time_shift<59)
...
while($time_shift<57 & time()-$start_time<57)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question