const DEVELOPER_INFO = [
" autor " => " Matheus Johann Araújo " ,
" country " => " Brasil " ,
" state " => " Pernambuco " ,
" date " => " 2022-03-12 "
]; คำว่า callback หมายถึงฟังก์ชั่นที่ผ่านมาเป็นพารามิเตอร์ของฟังก์ชันซึ่งจะถูกเรียกโดยฟังก์ชั่น ใน PHP callbacks เป็นประเภท callable ซึ่งหมายถึง chamable;
ชั้นเรียนที่เรียกว่า Closure มีหน้าที่แสดงฟังก์ชั่นที่ไม่ระบุชื่อและ arrow functions (ฟังก์ชั่นลูกศร);
callback มักจะเป็นฟังก์ชั่นที่ไม่มีชื่อ (ฟังก์ชั่นที่ไม่ระบุชื่อหรือลูกศร) ที่ส่งผ่านโดยพารามิเตอร์ แต่ไม่มีอะไรป้องกันไม่ให้ฟังก์ชั่นชื่อถูกส่งผ่านเป็นพารามิเตอร์!
setInterval(callback, milliseconds) ทำหน้าที่เรียกใช้การโทรกลับในเวลาที่ได้รับข้อมูลอย่างไม่ จำกัด ฟังก์ชั่นส่งคืน UID ที่สามารถใช้ในฟังก์ชัน clearInterval เพื่อลบ setInterval ออกจากบรรทัดการดำเนินการ;
setTimeout(callback, milliseconds) ดำเนินการเรียกใช้ฟังก์ชั่นการโทรกลับในเวลาที่ได้รับข้อมูลในวิธีที่ไม่ซ้ำกัน ฟังก์ชั่นส่งคืน UID ที่สามารถใช้ในฟังก์ชั่น clearTimeout เพื่อลบ setTimeout ออกจากบรรทัดการดำเนินการ;
clearInterval(UID) เสร็จสิ้นการดำเนินการในอนาคตของ setInterval ที่มี UID ที่ได้รับข้อมูล ฟังก์ชั่นส่งคืน true (เสร็จสิ้น) หรือ false (ไม่เสร็จหรือไม่พบงานที่กำหนด);
clearTimeout(UID) สรุปการดำเนินการในอนาคตของ setTimeout ที่มี UID ที่ได้รับการแจ้ง ฟังก์ชั่นส่งคืน true (เสร็จสิ้น) หรือ false (ไม่เสร็จหรือไม่พบงานที่กำหนด)
<?php
// EN-US: Include at the beginning of the first file to be interpreted, on the WEB server use TICK sparingly
// PT-BR: Incluir no início do primeiro arquivo a ser interpretado, no servidor WEB use o TICK com moderação
declare (ticks= 1 );
require_once " lib/code.php " ;
echo " Start " , PHP_EOL ;
$ counter = 1 ;
$ uid = setInterval ( function () use (& $ counter ) {
echo " Counter: " , $ counter ++, PHP_EOL ;
}, 100 );
setTimeout ( function () {
echo " Half of the increments " , PHP_EOL ;
}, 1000 );
setTimeout ( function () use ( $ uid ) {
echo " Stopping the counter " , PHP_EOL ;
clearInterval ( $ uid );
}, 2000 );
echo " Processing... " , PHP_EOL ;
// EN-US: Include after timed calls
// PT-BR: Incluir após chamadas programadas (agendadas)
$ count = workWait ( function () { usleep ( 1 ); });
echo " workRun has been run $ {count} times " , PHP_EOL ;
echo " End " , PHP_EOL ; then(callback) ที่เรียกว่าเมื่อ สัญญา ได้รับการแก้ไข callback จะถูกดำเนินการและจะได้รับเป็นพารามิเตอร์ค่าที่ส่งผ่านในฟังก์ชัน resolve
วิธี catch(callback) ที่เรียกว่าเมื่อ สัญญา ถูกปฏิเสธ callback จะดำเนินการและจะได้รับเป็นพารามิเตอร์ค่าที่ส่งผ่านในฟังก์ชัน reject
finally(callback) ที่เรียกว่าหลังจาก สัญญา ได้รับการแก้ไขหรือปฏิเสธซึ่งยิงการดำเนินการของ callback ที่ได้รับแจ้งว่าเป็นพารามิเตอร์
<?php
// EN-US: Include at the beginning of the first file to be interpreted, on the WEB server use TICK sparingly
// PT-BR: Incluir no início do primeiro arquivo a ser interpretado, no servidor WEB use o TICK com moderação
declare (ticks= 1 );
require_once " lib/code.php " ;
echo " Start " , PHP_EOL ;
$ promise = new Promise ( function ( $ resolve , $ reject ) {
$ call = rand ( 0 , 1 ) ? $ resolve : $ reject ;
setTimeout ( function () use ( $ call ) {
$ call ( " message " );
}, 1000 );
});
function info_promise () {
global $ promise ;
echo " > monitor: " , $ promise -> getMonitor (), PHP_EOL ;
echo " > state: " , $ promise -> getState (), PHP_EOL ;
}
info_promise ();
$ promise -> then ( function ( $ result ) {
echo " then ( $ {result} ) " , PHP_EOL ;
info_promise ();
})-> catch ( function ( $ error ) {
echo " catch ( $ {error} ) " , PHP_EOL ;
info_promise ();
})-> finally ( function () {
echo " finally " , PHP_EOL ;
info_promise ();
});
echo " Processing... " , PHP_EOL ;
for ( $ counter = 0 ; $ counter < 10 ; $ counter ++) {
echo " Counter: $ {counter}" , PHP_EOL ;
usleep ( 200000 );
}
// EN-US: Include after timed calls
// PT-BR: Incluir após chamadas programadas (agendadas)
$ count = workWait ( function () { usleep ( 1 ); });
echo " workRun has been run $ {count} times " , PHP_EOL ;
echo " End " , PHP_EOL ;