简体   繁体   English

一页中的多个倒数时间

[英]multiple countdown time in one page

first of all , i work with jalali/persian date and countdown jQuery plugins does not work for me. 首先,我使用jalali /波斯日期和倒数jQuery插件对我不起作用。 for ex plugins must enter gregorian date like 2017/5/2 but persian/jalali date is 1396/2/17 . 对于前插件,必须输入gregorian日期(如2017/5/2),但波斯语/ jalali日期是1396/2/17。

i have muliple datetime with specify day , hour , min and sec . 我有指定日期,小时,分钟和秒的日期时间。 ex: 例如:

3 days 13 hour 4min 25 sec 3天13小时4分25秒

23 days 2hour 41 min 3 sec ... 23天2小时41分3秒...

i have days and hour and min and sec separate, 我的天和小时分别是分钟和秒,

i want count down for they dates in one page (number of they dates are daynamic) 我想倒数一页的日期(他们的日期是白天的)

i use this code and work fine when only have one date in page 我使用此代码,并且在页面中只有一个日期时可以正常工作

html : html:

  <div class="row">

@{TimeSpan span = (item.DiscountExpireDate - DateTime.Now);}

    <ul class="countdown">
        <li>
             <span class="seconds">@span.Seconds</span>
        <p class="seconds_ref">ثانیه</p>
        </li>
         <li class="seperator">:</li>
         <li>
         <span class="minutes">@span.Minutes</span>
           <p class="minutes_ref">دقیقه</p>
           </li>
           <li class="seperator">:</li>

           <li>
          <span class="hours">@span.Hours</span>
            <p class="hours_ref">ساعت</p>
           </li>
           <li class="seperator"> </li>
           <li>
           <span class="days">@span.Days</span>
         <p class="days_ref">روز</p>
         </li>
         </ul>

</div>

and js 和js

<script type="text/javascript">//for countdown
    $(document)
        .ready(function () {
            var theDaysBox = $(".days");
            var theHoursBox = $(".hours");
            var theMinsBox = $(".minutes");
            var theSecsBox = $(".seconds");

                var refreshId = setInterval(function() {
                        var currentSeconds = theSecsBox.text();
                        var currentMins = theMinsBox.text();
                        var currentHours = theHoursBox.text();
                        var currentDays = theDaysBox.text();

                        if (currentSeconds == 0 && currentMins == 0 && currentHours == 0 && currentDays == 0) {
                            // if everything rusn out our timer is done!!
                            // do some exciting code in here when your countdown timer finishes

                        } else if (currentSeconds == 0 && currentMins == 0 && currentHours == 0) {
                            // if the seconds and minutes and hours run out we subtract 1 day
                            theDaysBox.html(currentDays - 1);
                            theHoursBox.html("23");
                            theMinsBox.html("59");
                            theSecsBox.html("59");
                        } else if (currentSeconds == 0 && currentMins == 0) {
                            // if the seconds and minutes run out we need to subtract 1 hour
                            theHoursBox.html(currentHours - 1);
                            theMinsBox.html("59");
                            theSecsBox.html("59");
                        } else if (currentSeconds == 0) {
                            // if the seconds run out we need to subtract 1 minute
                            theMinsBox.html(currentMins - 1);
                            theSecsBox.html("59");
                        } else {
                            theSecsBox.html(currentSeconds - 1);
                        }
                    },
                    1000);

       });
</script>

but when i have multiple dates on page does not work well , and it seems that numbers are sum together. 但是,当我在页面上有多个日期时,效果不佳,并且看来数字是总和。

https://jsfiddle.net/a7ucv468/1/ https://jsfiddle.net/a7ucv468/1/

anyone can help me ? 有人可以帮助我吗?

You just need to identify a wrapper element for each one like: 您只需要为每个包装元素标识一个包装元素,例如:

$('.countdown").each( function {

Then use $(this).find() in front of your element selectors. 然后在元素选择器前面使用$(this).find()。 That will bind the functions to each instance of the countdown. 这会将功能绑定到倒数的每个实例。 Currently it is just looking for the class "days" so it'll just use the first one it finds, or cause other bugs. 当前,它只是在寻找“ days”类,因此它将仅使用找到的第一个类,否则会导致其他错误。 I'll write a working example soon. 我将尽快写一个工作示例。

I'm not sure about the numbers but they seem to be running independently here (set the timeout to less for debugging) https://jsfiddle.net/7nzcdhn3/ 我不确定这些数字,但它们似乎在这里独立运行(将超时设置为较小以进行调试) https://jsfiddle.net/7nzcdhn3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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