Ev EvTimer::__construct
(PECL ev >= 0.2.0)
EvTimer::__construct — EvTimer 감시자 객체를 생성합니다.
설명
public EvTimer::__construct( float $after , float $repeat , callable $callback , mixed $data = null , int $priority = 0 )
EvTimer 감시자 개체를 생성합니다.
매개변수
after
- 몇 초
after
에 트리거되도록 타이머를 구성합니다. repeat
- repeat가
0.0
이면 시간 초과에 도달하면 자동으로 중지됩니다. 양수이면 타이머가 수동으로 중지될 때까지 몇 초마다 반복적으로 다시 트리거되도록 자동으로 구성됩니다. callback
- Watcher callbacks을 참조하십시오.
data
- 감시자와 연결된 사용자 지정 데이터입니다.
priority
- Watcher priority
Examples
예제 #1 Simple timers
<?php
// Create and start timer firing after 2 seconds
$w1 = new EvTimer(2, 0, function () {
echo "2 seconds elapsed\n";
});
// Create and launch timer firing after 2 seconds repeating each second
// until we manually stop it
$w2 = new EvTimer(2, 1, function ($w) {
echo "is called every second, is launched after 2 seconds\n";
echo "iteration = ", Ev::iteration(), PHP_EOL;
// Stop the watcher after 5 iterations
Ev::iteration() == 5 and $w->stop();
// Stop the watcher if further calls cause more than 10 iterations
Ev::iteration() >= 10 and $w->stop();
});
// Create stopped timer. It will be inactive until we start it ourselves
$w_stopped = EvTimer::createStopped(10, 5, function($w) {
echo "Callback of a timer created as stopped\n";
// Stop the watcher after 2 iterations
Ev::iteration() >= 2 and $w->stop();
});
// Loop until Ev::stop() is called or all of watchers stop
Ev::run();
// Start and look if it works
$w_stopped->start();
echo "Run single iteration\n";
Ev::run(Ev::RUN_ONCE);
echo "Restart the second watcher and try to handle the same events, but don't block\n";
$w2->again();
Ev::run(Ev::RUN_NOWAIT);
$w = new EvTimer(10, 0, function() {});
echo "Running a blocking loop\n";
Ev::run();
echo "END\n";
?>
위의 예는 다음과 유사한 결과를 출력합니다.
2 seconds elapsed is called every second, is launched after 2 seconds iteration = 1 is called every second, is launched after 2 seconds iteration = 2 is called every second, is launched after 2 seconds iteration = 3 is called every second, is launched after 2 seconds iteration = 4 is called every second, is launched after 2 seconds iteration = 5 Run single iteration Callback of a timer created as stopped Restart the second watcher and try to handle the same events, but don't block Running a blocking loop is called every second, is launched after 2 seconds iteration = 8 is called every second, is launched after 2 seconds iteration = 9 is called every second, is launched after 2 seconds iteration = 10 END
기타
- EvTimer::createStopped() - EvTimer 정지된 감시자 객체를 생성합니다.
- EvPeriodic
- » ev_timer - relative and optionally repeating timeouts
- » Be smart about timeouts