【发布时间】:2018-08-06 05:07:47
【问题描述】:
我想根据服务器响应启动倒计时。我首先为本地测试制作了一个计时器,在该测试中我通过了员工姓名、小时、分钟和秒。它工作正常。但现在我想将倒数计时器作为服务器数据启动。它将在一分钟内获取员工姓名和倒数计时器时间。 但它没有正确拆分 json 对象。它显示 var 有未定义的数据。
API 响应-
[{"EmployeeName":"Abc","Hour":9,"Min":25},{"EmployeeName":"Xyz","Hour":11,"Min":41}]
Javascript
function GetMachine() {
var http = new XMLHttpRequest();
var url = 'php/StitchTimer.php';
http.open('GET', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
var data = http.responseText;
var jsonResponse = JSON.parse(data);
for(var i=0;i<jsonResponse.length;i++){
var index = jsonResponse[i];
console.log(index);
var empname = index["EmployeeName"];
var hour = index["Hour"];
var minute = index["Min"];
addEmployee(empname,hour,minute);
}
}
http.send();
}
}
function addEmployee(emp,hr,mi)
{
var employee = new Employee(emp,hr,mi,00);
employeeList.appendChild(employee.domObj);
employee.startTimer();
}
class Employee
{
constructor(name,hr,min,sec)
{
var self=this;
this.timer;
this.timeInSec;
this.domObj=document.createElement("div");
this.timeSpan=document.createElement("span");
this.domObj.style.backgroundColor = '#4CA';
this.domObj.style.border = 'none';
this.domObj.style.height = '100px';
this.domObj.style.width = '100px';
this.domObj.style.color = 'white';
this.domObj.style.padding = '20px';
this.domObj.style.textAlign = 'center';
this.domObj.style.textDecoration = 'none';
this.domObj.style.display = 'inline-block';
this.domObj.style.fontSize = '26px';
this.domObj.style.borderRadius = '50%';
this.domObj.style.margin = '20px';
this.domObj.style.justifyContent = "center";
this.timeInSec=hr*60*60+min*60+parseInt(sec);
this.timeSpan.innerHTML=hr+":"+min+":"+sec;
this.domObj.innerHTML=name+"<br>";
this.domObj.appendChild(this.timeSpan);
console.log("0:"+this.timeInSec);
}
startTimer()
{
this.timer=setInterval(this.updateTime.bind(this),1000);
}
updateTime()
{
var hr,min,sec,temp;
if (this.timeInSec<=0)
{
clearInterval(this.timer);
}
else
{
this.timeInSec--;
//console.log("1:"+this.timeInSec);
sec=this.timeInSec % 60;
temp=this.timeInSec-sec;
temp/=60;
//console.log("2:"+temp);
min=temp % 60;
temp-=min;
hr=temp/60;
this.timeSpan.innerHTML=hr+":"+min+":"+sec;
}
}
}
PHP 代码
<?php
date_default_timezone_set('Asia/Kolkata');
$con = mysqli_connect("localhost","username","pw","db");
$currenttime = date("Y-m-d H:i");
$sql = "SELECT * FROM `Stitch` WHERE `EndTime` > '$currenttime' ";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res))
{
$end = $row['EndTime'];
$end = strtotime($end);
$current = strtotime($currenttime);
$timer = $end-$current;
$timer = $timer/60;
$hour = floor($timer/60);
$min = $timer-($hour*60);
$result[]=array('EmployeeName' =>$row['WorkerName'],'Hour'=>$hour,'Min'=>$min);
}
echo json_encode($result);
mysqli_close($con);
?>
【问题讨论】:
-
“问题出在构造函数中,我必须通过 Employenname, hour,min,sec 所有的东西” - 所以改变构造函数的要求来满足你的需要?
-
在 JS 中,如果您不想传递某些值,可以将
undefined传递给函数参数。所以如果你没有传递名字,hr,sec 做这样的事情,new Employee('', 0, minute.value, 0);- 我没有更详细地检查你的代码。我不确定避免为员工命名有多好。您必须处理的其他验证内容。 -
现在我更改我的 php 代码,现在我在 json 数组中传递员工姓名、小时和分钟。现在我想捕获它并将其存储在 js 变量中。
-
现在检查我的问题@MagnusEriksson
-
检查你调用 addEmployee() 的 for 循环。它是 inifinit 循环。
标签: javascript php jquery