我正在从事柜台服务项目,我需要控制客户的流量,每个客户可以有5分钟在柜台。另一方面,系统应控制5个计数器。
这是我尝试解决这个问题的方法。看看我的MWE:
//this is part of the code where I set the timer for each counter.
function increment() {
if (running == 1) {
setTimeout(function() {
time++;
var mins = Math.floor(time / 10 / 60);
var secs = Math.floor(time / 10);
var tenths = time % 10;
if (mins == 5) {
//after 5 mins is over the system should call php deQueue() method to dequeue the current and point out to next customer.
document.getElementById("message").innerHTML = "Currently, Counters are available... Next Customer..."
deQueue();
//this is my php method.
available();
}
document.getElementById("timer").innerHTML = mins + ":" + secs + ":" + tenths;
increment();
}, 100)
}
}//And this is my php file.
<?php
public function deQueue(){
if (!$this->isEmpty()) {
$this->front = ($this->front+1) %5;
$this->size--;
}else{
echo "The Counter is empty! <br>";
}
}
?>
发布于 2019-03-23 12:14:45
现在您需要对php文件使用ajax请求,如下所示:
function increment() {
if (running == 1) {
setTimeout(function() {
time++;
var mins = Math.floor(time / 10 / 60);
var secs = Math.floor(time / 10);
var tenths = time % 10;
if (mins == 5) ajaxDequeue(mins);
document.getElementById("timer").innerHTML = mins + ":" + secs + ":" + tenths;
increment();
}, 100)
}
}
function ajaxDequeue(mins){
$.ajax({
url: 'ajax.php?minutes=' + mins,
beforeSend: function()
{
document.getElementById("message").innerHTML = "Currently, Counters are available... Next Customer..."
},
success: function(response)
{
document.getElementById("message").innerHTML = response;
}
});
}然后在ajax.php文件中将其设置为检查分钟值
<?php
$minutes = $_GET["minutes"];
if (isset($minutes)) deQueue();
public function deQueue(){
if (!$this->isEmpty()) {
$this->front = ($this->front+1) %5;
$this->size--;
}else{
echo "The Counter is empty! <br>";
}
}
?>发布于 2019-03-23 11:49:15
在我看来,您正在尝试直接从JavaScript调用PHP函数。为了用简单的话来描述,这两种语言通常需要一个额外的层来通信,称为HTTP,因为PHP运行在and服务器上,而JavaScript运行在and浏览器上。
要“在一段时间后”执行JavaScript函数,可以使用setTimeout。
要打开与PHP (webserver)的通信,您可以使用AJAX。
https://stackoverflow.com/questions/55310336
复制相似问题