[英]Showing countdown timer on each foreach
我試圖在頁面上為foreach
語句的每個循環放置一個倒數計時器。 目前,它似乎只顯示在最后一個循環中。
這是我的foreach
:
@foreach($top4auctions as $auction)
<div class="span3">
<a href="/auction/{{ $auction->id }}"><u><strong>{{ $auction->item }}</strong></u>
<br />
Current Price: ${{ $auction->price }}
<div class="span8">
<img src="{{ $auction->itempic }}" alt="{{ $auction->item }} CS:GO Skin Auction"></a>
<br />
<div class="clock{{ $auction->id }}"></div>
<script>
window.onload = function () {
var fiveMinutes{{ $auction->id }} = 60 * 5,
display = document.querySelector('.clock{{ $auction->id }}');
startTimer(fiveMinutes{{ $auction->id }}, display);
};
</script>
</div>
</div>
@endforeach
這是startTimer
的代碼:
<script type="text/javascript">
function startTimer(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
// get the number of seconds that have elapsed since
// startTimer() was called
diff = duration - (((Date.now() - start) / 1000) | 0);
// does the same job as parseInt truncates the float
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (diff <= 0) {
// add one second so that the count down starts at the full duration
// example 05:00 not 04:59
start = Date.now() + 1000;
}
};
// we don't want to wait a full second before the timer starts
timer();
setInterval(timer, 1000);
}
</script>
我已經根據$auction->id
命名了所有東西,但它仍然不起作用..所以我現在不知道該怎么做。
而不是覆蓋每個循環的window.onload,請嘗試以下操作
<script>
window.addEventListener('load', function() {
var fiveMinutes {
{
$auction - > id
}
} = 60 * 5,
display = document.querySelector('.clock{{ $auction->id }}');
startTimer(fiveMinutes {
{
$auction - > id
}
}, display);
});
</script>
以前的解決方案是好的,這里是整理
<script> window.addEventListener('load', function() { var fiveMinutes{{ $product->id}} = 60*5, display = document.querySelector('.clock{{ $product->id }}'); startTimer(fiveMinutes{{$product->id}}, display); }); </script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.