gpt4 book ai didi

javascript - 在每个 foreach 上显示倒计时器

转载 作者:行者123 更新时间:2023-12-02 16:00:38 24 4
gpt4 key购买 nike

我试图在页面上为 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{{ $product->id}} = 60*5,
display = document.querySelector('.clock{{ $product->id }}');
startTimer(fiveMinutes{{$product->id}}, display);
});
</script>

关于javascript - 在每个 foreach 上显示倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31239280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com