![]() HTML_Progress2 : The Definitive Guide
|
Suppose we decide to send a mail after a process is over (progress meter reach 100%) and keep aware some users. We will see how to do this now.
First step is to declare a function (user callback) that will observe all progress meter events.
Interface of such function (callback) is pretty easy. It requires only one argument passed by reference ($notification
) and that should contains a
PEAR::Event_Dispatcher instance.
Here are, in purpose of our tutorial, a function that will observe events and send a mail, at end of process, to webmaster@site.com with a short message that gave time elapsed.
Second step is to attach this function (user callback) to the progress meter with the
addListener()
method.
<?php require_once 'HTML/Progress2.php'; function getmicrotime($time) { list($usec, $sec) = explode(' ', $time); return ((float)$usec + (float)$sec); } function myObserver(&$notification){ static $time_start; $notifyName = $notification->getNotificationName();
$notifyInfo = $notification->getNotificationInfo();
switch ($notifyName) { case 'onSubmit':
$time_start = getmicrotime($notifyInfo['time']); break; case 'onLoad':
$time_elapse = getmicrotime($notifyInfo['time']) - $time_start; error_log ('process ID=5 is over (elapse time = '. $time_elapse . ' sec.)', 3, 'progress_observer.log'); break; } } $pb = new HTML_Progress2(); $pb->setAnimSpeed(200); $pb->setIncrement(10); $pb->addListener('myObserver');
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Observer Pattern</title> <?php echo $pb->getStyle(false); echo $pb->getScript(false); ?> </head> <body> <?php $pb->display(); $pb->run(); ?> </body> </html>
Third step (optional) is stop listen events and then detach user function (callback) to the progress meter with the
removeListener()
method.
![]() |
Note |
---|---|
While there are any observers attached, progress meter will continue to notify them of each event when they occured. |
HTML_Progress2 : The Definitive Guide | v 1.0.0 : September 23, 2005 |