JavaScript中的iPhone计时器给了我NaN吗?

时间:2018-09-04 09:56:37

标签: javascript iphone timer

对不起我的英语,我尽力了:D

我在iPhone设备上的计时器有问题

它给了我NaN

所以当我在android上打开它的工作时,但在safari或ipod上却不起作用

我该如何解决?

对不起我的英语,我尽力了:D

我在iPhone设备上的计时器有问题

它给了我NaN

所以当我在android上打开它的工作时,但在safari或ipod上却不起作用

我该如何解决?

//timer
function updater(d, h, m, s , baseTime) {
  // День сброса - 27 сентября 2015 года (и далее каждые три дня)  
  function update() {
    var cur = new Date();
    // сколько осталось миллисекунд
    var diff = baseTime - cur;
    if(cur > baseTime){

   }else{
    // сколько миллисекунд до конца секунды
    var millis = diff % 1000;

    diff = Math.floor(diff/1000);
    // сколько секунд до конца минуты
    var sec = diff % 60;
    if(sec < 10) sec = "0"+sec;
    diff = Math.floor(diff/60);
    // сколько минут до конца часа
    var min = diff % 60;
    if(min < 10) min = "0"+min;
    diff = Math.floor(diff/60);
    // сколько часов до конца дня
    var hours = diff % 24;
    var days = Math.floor(diff / 24);
    
    if(days != 0) d.text(days + " :");
    if( hours != 0) h.text(hours + " :");
    m.text(min + " :");
    s.text(sec);
    
    
    // следующий раз вызываем себя, когда закончится текущая секунда
    setTimeout(update, millis);
  }
}
setTimeout(update, 0);  
}

        var d = $("#days1");
        $('.block_timer').each(function () {
          var d1 = $(this).children(".day").attr("id");
          d = $("#"+d1);
          var h1 = $(this).children(".hour").attr("id");
          h = $("#"+h1);
          var m1 = $(this).children(".minute").attr("id");
          m = $("#"+m1);
          var s1 =$(this).children(".second").attr("id");
          s = $("#"+s1);
          var ddd = $(this).children(".timer_value").val();
          updater(d,h,m,s,new Date(ddd));
        });

//timer
function updater(d, h, m, s , baseTime) {
  // День сброса - 27 сентября 2015 года (и далее каждые три дня)  
  function update() {
    var cur = new Date();
    // сколько осталось миллисекунд
    var diff = baseTime - cur;
    if(cur > baseTime){

   }else{
    // сколько миллисекунд до конца секунды
    var millis = diff % 1000;

    diff = Math.floor(diff/1000);
    // сколько секунд до конца минуты
    var sec = diff % 60;
    if(sec < 10) sec = "0"+sec;
    diff = Math.floor(diff/60);
    // сколько минут до конца часа
    var min = diff % 60;
    if(min < 10) min = "0"+min;
    diff = Math.floor(diff/60);
    // сколько часов до конца дня
    var hours = diff % 24;
    var days = Math.floor(diff / 24);
    
    if(days != 0) d.text(days + " :");
    if( hours != 0) h.text(hours + " :");
    m.text(min + " :");
    s.text(sec);
    
    
    // следующий раз вызываем себя, когда закончится текущая секунда
    setTimeout(update, millis);
  }
}
setTimeout(update, 0);  
}

        var d = $("#days1");
        $('.block_timer').each(function () {
          var d1 = $(this).children(".day").attr("id");
          d = $("#"+d1);
          var h1 = $(this).children(".hour").attr("id");
          h = $("#"+h1);
          var m1 = $(this).children(".minute").attr("id");
          m = $("#"+m1);
          var s1 =$(this).children(".second").attr("id");
          s = $("#"+s1);
          var ddd = $(this).children(".timer_value").val();
          updater(d,h,m,s,new Date(ddd));
        });
<table class="airdrop_rating"><thead><tr><td></td><td class="airdrop_rating_name">НАЗВАНИЕ</td><td>Стартовая цена</td><td>Время ICO</td><td>Цель</td><td>Рейтинг</td></tr></thead><tbody><tr><td class="airdrop_num">1.</td><td class="airdrop_logo">
<img width="25" height="25" src="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?resize=25%2C25&amp;ssl=1" class="attachment-25_25 size-25_25 jetpack-lazy-image--handled" alt="" data-attachment-id="154" data-permalink="https://bitinvestnews.com/ico_siocoin-1c2d51d44b9329ecf1ef171be3b22505/" data-orig-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?fit=125%2C125&amp;ssl=1" data-orig-size="125,125" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505" data-image-description="" data-medium-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?fit=125%2C125&amp;ssl=1" data-large-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?fit=125%2C125&amp;ssl=1" data-lazy-loaded="1" srcset="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?w=125&amp;ssl=1 125w, https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?resize=25%2C25&amp;ssl=1 25w, https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/ico_siocoin.1c2d51d44b9329ecf1ef171be3b22505.jpg?resize=120%2C120&amp;ssl=1 120w" sizes="(max-width: 25px) 100vw, 25px">              <a class="airdrop_href" href="https://bitinvestnews.com/ico/sio-coin/">SiO Coin</a></td><td>USD 1 000 000</td><td><div class="block_timer">
осталось:<br>
<input type="hidden" class="timer_value" value="2018,9,30">
<span class="day" id="days100">25 :</span>
<span class="hour" id="hours100">11 :</span>
<span class="minute" id="minutes100">21 :</span>
<span class="second" id="seconds100">50</span></div></td><td>$1,000,000</td><td class="rating_scale" style="background-color:#78e465"><span>53/100</span></td><td></td></tr><tr><td class="airdrop_num">2.</td><td class="airdrop_logo">
<img width="25" height="25" src="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?resize=25%2C25&amp;ssl=1" class="attachment-25_25 size-25_25 jetpack-lazy-image--handled" alt="" data-attachment-id="171" data-permalink="https://bitinvestnews.com/wt/" data-orig-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?fit=125%2C125&amp;ssl=1" data-orig-size="125,125" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wt" data-image-description="" data-medium-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?fit=125%2C125&amp;ssl=1" data-large-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?fit=125%2C125&amp;ssl=1" data-lazy-loaded="1" srcset="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?w=125&amp;ssl=1 125w, https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?resize=25%2C25&amp;ssl=1 25w, https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/wt.png?resize=120%2C120&amp;ssl=1 120w" sizes="(max-width: 25px) 100vw, 25px">              <a class="airdrop_href" href="https://bitinvestnews.com/ico/exmo-coin/">Exmo Coin</a></td><td>USD 1 000 000</td><td><div class="block_timer">
<input type="hidden" class="timer_value" value="2018,6,26">
<span class="day" id="days93"></span>
<span class="hour" id="hours93"></span>
<span class="minute" id="minutes93"></span>
<span class="second" id="seconds93"></span></div></td><td>$3,000,000</td><td class="rating_scale" style="background-color:#fff0b6"><span>50/100</span></td><td></td></tr><tr><td class="airdrop_num">3.</td><td class="airdrop_logo">
<img width="25" height="25" src="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?resize=25%2C25&amp;ssl=1" class="attachment-25_25 size-25_25 jetpack-lazy-image--handled" alt="Satisfaction Token logo" data-attachment-id="974" data-permalink="https://bitinvestnews.com/satisfaction-token/" data-orig-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?fit=125%2C125&amp;ssl=1" data-orig-size="125,125" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Satisfaction Token" data-image-description="" data-medium-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?fit=125%2C125&amp;ssl=1" data-large-file="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?fit=125%2C125&amp;ssl=1" data-lazy-loaded="1" srcset="https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?w=125&amp;ssl=1 125w, https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?resize=25%2C25&amp;ssl=1 25w, https://i2.wp.com/bitinvestnews.com/wp-content/uploads/2018/08/satisfaction-token.png?resize=120%2C120&amp;ssl=1 120w" sizes="(max-width: 25px) 100vw, 25px">              <a class="airdrop_href" href="https://bitinvestnews.com/ico/exmo-coin-2/">Satisfaction Token</a></td><td>USD 1 000 000</td><td><div class="block_timer">
начало через:<br>
<input type="hidden" class="timer_value" value="2018,6,23">
<span class="day" id="days95"></span>
<span class="hour" id="hours95"></span>
<span class="minute" id="minutes95"></span>
<span class="second" id="seconds95"></span></div></td><td>$3,000,000</td><td class="rating_scale" style="background-color:#fff0b6"><span>47/100</span></td><td></td></tr><tr><td class="airdrop_num">4.</td><td class="airdrop_logo">
<img width="25" height="25" src="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?resize=25%2C25&amp;ssl=1" class="attachment-25_25 size-25_25 jetpack-lazy-image--handled" alt="" data-attachment-id="155" data-permalink="https://bitinvestnews.com/kep-eb573c0084a8fcabc33e874305968b37/" data-orig-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?fit=125%2C125&amp;ssl=1" data-orig-size="125,125" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="kep.eb573c0084a8fcabc33e874305968b37" data-image-description="" data-medium-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?fit=125%2C125&amp;ssl=1" data-large-file="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?fit=125%2C125&amp;ssl=1" data-lazy-loaded="1" srcset="https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?w=125&amp;ssl=1 125w, https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?resize=25%2C25&amp;ssl=1 25w, https://i1.wp.com/bitinvestnews.com/wp-content/uploads/2018/06/kep.eb573c0084a8fcabc33e874305968b37.png?resize=120%2C120&amp;ssl=1 120w" sizes="(max-width: 25px) 100vw, 25px">              <a class="airdrop_href" href="https://bitinvestnews.com/ico/kepler/">Kepler</a></td><td>USD 1 000 000</td><td><div class="block_timer">
<input type="hidden" class="timer_value" value="2018,6,22">
<span class="day" id="days98"></span>
<span class="hour" id="hours98"></span>
<span class="minute" id="minutes98"></span>
<span class="second" id="seconds98"></span></div></td><td>$1,000,000</td><td class="rating_scale" style="background-color:#ff8f8f"><span>22/100</span></td><td></td></tr></tbody></table>

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

var ddd = $(this).children(".timer_value").val();
updater(d,h,m,s,new Date(ddd));

ddd 的值从此处返回:

<input type="hidden" class="timer_value" value="2018,9,30">

因此,您拥有的实际上是:

new Date('2018,9,30') 

在某些实现中会导致无效的Date,其时间值为NaN。参见Why does Date.parse give incorrect results?

您可以使用以下简单的解析功能对其进行修复:

function parseIt(s) {
  var b = s.split(/\D/);
  return new Date(b[0], b[1]-1, b[2]);
}

和:

updater(d,h,m,s, parseIt(ddd));

此答案将很快被删除...