
在上面的图片中,我有列Check-in hours和Time Left,这是一个基于Check-in hours列的倒计时器。
如您所见,第一行只有2小时和36分钟,这是基于具有3小时的Check in hours列中的。这是正确的。
然而,第二行还有2小时和36分钟,但是check in hours值为12 hours。
下面是我的JS代码:
function makeTimer() {
var ROOMid = $("#row_id").text();
$.get('/get-time-out?id=' + ROOMid,function(data){
hms = data[0].time_out + " GMT+08:00";
console.log(hms)
});
var endTime = new Date(hms);
endTime = (Date.parse(endTime) / 1000);
var now = new Date();
now = (Date.parse(now) / 1000);
var timeLeft = endTime - now;
var days = Math.floor(timeLeft / 86400);
var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));
if(days == 0 && hours == 0 && 1 > minutes){
$("#days").html("<strong><u>TIME OUT</u></strong>");
$("#minutes").html("");
$("#hours").html("");
$("#seconds").html("");
}else if(0 > days){
$("#days").html("<strong><u>TIME OUT</u></strong>");
$(".minutes").html("");
$(".hours").html("");
$(".seconds").html("");
}else{
$(".days").html(days + " days");
$(".hours").html(hours + " hours");
$(".minutes").html(minutes + " minutes");
$(".seconds").html(seconds + " seconds");
}
}这是我的blade.php
@foreach ($customers as $c)
<tr>
<td hidden class="row_id" id="row_id">{{$c->id}}</td>
<td scope="row" class="font-weight-bold">{{$c->room}}</td>
<td>{{$c->room_type}}</td>
<td>{{$c->name}}</td>
<td>{{$c->check_in_hrs}}</td>
<td id="time">
{{
$ymd = DateTime::createFromFormat('d F Y H:i:s', $c->time_out)->format('d F y h:i A')
}}</td>
<td>
<div id="timer" class="timer">
<div class="days timer">-</div>
<div class="hours timer">-</div>
<div class="minutes timer">-</div>
<div class="seconds timer">-</div>
</div>
</td>
<td>{{$c->assistant}}</td>
<td>
<i class="fa fa-plus-circle lblue fa-2x" aria-hidden="true"></i>
<i class="fa fa-times red fa-2x" aria-hidden="true"></i>
</td>
</tr>
@endforeach发布于 2019-12-02 09:28:16
代码中的问题是该函数独立于任何其他参数,因此在这两种情况下,它都返回相同的结果。
您想要做的是重写代码逻辑,以接受至少一个参数,该参数将确定需要计算多少小时才能返回行。
有点像
function makeTimer(timeLeft) {
var ROOMid = $("#row_id").text();
$.get('/get-time-out?id=' + ROOMid,function(data){
hms = data[0].time_out + " GMT+08:00";
console.log(hms)
});
var endTime = new Date(hms);
endTime = (Date.parse(endTime) / 1000);
var now = new Date();
now = (Date.parse(now) / 1000);
var timeLeft = endTime - now;然后,在需要它的代码中,将小时数作为timeLeft参数传递到函数中。
https://stackoverflow.com/questions/59135821
复制相似问题