duedate计数器适用于Chrome但不支持Firefox NaN错误

时间:2017-02-09 03:41:14

标签: javascript jquery html ruby-on-rails firefox

当涉及到这个js代码时,Firefox无法正常工作。但它在Chrome中运行良好。

  • 这是否与document.getElementById("days").innerText = d;

  • 有关
  • 我需要parseInt吗? idk如果我必须这样做怎么做..哈哈

  • 主要问题是如何让它在firefox中运行?



 <div id="dueDate-counter">
    	<table>
    		<tr>
    			<td>days</td>
    			<td>hrs</td>
    			<td>mns</td>
    			<td>secs</td>
    		</tr>
    		<tr>
    			<td id="days"></td>
    			<td id="hours"></td>
    			<td id="minutes"></td>
    			<td id="seconds"></td>
    		</tr>

    	</table> 

    	<script type="text/javascript">
    		function countdown() {
    			var now = new Date();
    			var eventDate = new Date("<%= reservation.due_date %>");
    			<%= puts "here is reservation !!!!!!!" %>;
    			<%= puts "#{reservation.due_date}" %>;
    			var currentTime = now.getTime();
    			var eventTime = eventDate.getTime();

    			var remTime = eventTime - currentTime;

    			var s = Math.floor(remTime / 1000);
    			var m = Math.floor(s / 60);
    			var h = Math.floor(m / 60);
    			var d = Math.floor(h / 24);

    			h %= 24;
    			m %= 60;
    			s %= 60;

    			h = (h < 10) ? "0" + h : h;
    			m = (m < 10) ? "0" + m : m;
    			s = (s < 10) ? "0" + s : s;

    			document.getElementById("days").textContent = d;
    			document.getElementById("days").innerText = d;

    			document.getElementById("hours").textContent = h;
    			document.getElementById("minutes").textContent = m;
    			document.getElementById("seconds").textContent = s;

    				setTimeout(countdown, 1000);
    					}

    				countdown();

    	</script>

    </div>
&#13;
&#13;
&#13;

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

Chrome比其他具有Date对象的浏览器更灵活。确保due_dateeventDate的格式与console.log的格式相同。 getTime()返回毫秒,因此reservation.due_date也必须以毫秒为单位。