繁体   English   中英

在每个 foreach 上显示倒数计时器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM