linux5620207 2016-05-20 14:49 采纳率: 5.3%
浏览 2658

js 中 Date.now()相减会出现 NaN 的问题,求教

首先js代码如下
setTimeout(function() {
var a = 0,wdw1;
$($(".yscroll_list_left li")[0]).clone(!0).insertAfter($($(".yscroll_list_left li")[$(".yscroll_list_left li").length - 1]));
var len = $(".yscroll_list_left li").length;
b();
var old = Date.now();
function b() {
a -= 44;
if(a >= 44 * -(len - 2) ){
console.log("1_1",a,Date.now()-old);
$(".yscroll_list_left").animate({marginTop: a}, 2000);
console.log("1_2",a,Date.now()-old);
}
else{
$(".yscroll_list_left").animate({marginTop: a}, 2000, function() {
a = 0;
$(".yscroll_list_left").css({
marginTop: 0
})
});
}
wdw1 = setTimeout(b,3000);
}
}, 0);
在chrome中执行结果如下
test.html:42 1_1 -44 NaN
test.html:44 1_2 -44 NaN
test.html:42 1_1 -88 3001
test.html:44 1_2 -88 3003
test.html:42 1_1 -132 6003
test.html:44 1_2 -132 6004
test.html:42 1_1 -176 9004
test.html:44 1_2 -176 9005
上边的是一个不断循环重复的结果,只是以后的 -44后边的内容不再是NaN,而是具体数字了。
想不明白为什么Date.now()-old会得出NaN,如果是因为数太小的话,那么单独像下面一样
var old = Date.now();
console.log(Date.now() - old);
上述代码会输出 0 或者是 1,而非NaN
另外,在火狐上测试,结果和chrome一样,也是前两个 -44 后边为NaN,而以后的 -44后边就变为数字
上边的只是JS代码,下边是完整的代码,可以执行的
<!DOCTYPE html>


#yscroll_list{ height:16px; overflow:hidden; } #yscroll_listin{ position:relative; overflow:hidden; } #yscroll_listin ul { position:relative; margin-left:50px; } window.onload = function(){ setTimeout(function() { var a = 0,wdw1; $($(".yscroll_list_left li")[0]).clone(!0).insertAfter($($(".yscroll_list_left li")[$(".yscroll_list_left li").length - 1])); var len = $(".yscroll_list_left li").length; b(); var old = Date.now(); function b() { a -= 44; if(a >= 44 * -(len - 2) ){ console.log("1_1",a,Date.now()-old); $(".yscroll_list_left").animate({marginTop: a}, 500); console.log("1_2",a,Date.now()-old); } else{ $(".yscroll_list_left").animate({marginTop: a}, 500, function() { a = 0; $(".yscroll_list_left").css({ marginTop: 0 }) }); } wdw1 = setTimeout(b,1000); } }, 0); }


  • 111

  • 222

  • 333

  • 444

  • 555



  • 写回答

1条回答 默认 最新

  • havedream_one 2016-05-21 00:09
    关注
     <script language="javascript" type="text/javascript">
    function DateDiff(sDate1,sDate2){ 
    //sDate1和sDate2是年-月-日格式 
    var arrDate,objDate1,objDate2,intDays;
    arrDate=sDate1.split("-");
    objDate1=new Date(arrDate[1]+'-'+arrDate[2]+'-'+arrDate[0]);//转换为月-日-年格式 
    arrDate=sDate2.split("-");
    objDate2=new Date(arrDate[1] + '-'+arrDate[2]+'-'+arrDate[0]);
    intDays=parseInt(Math.abs(objDate1-objDate2)/1000/60/60/24); //把相差的毫秒数转换为天数 
    return intDays;
    }
    alert(DateDiff("2007-1-1","2008-1-1"));
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP