实现倒计时效果
//<div id='date'><div> var end = new Date('2021-1-1 00:00'); function countDone(){ var now = new Date(); var remainder = end - now; if(remainder<0){ return; } var day = Math.floor(remainder/1000/60/60/24); //天 var hour = Math.floor(remainder/1000/60/60%24); //时 var minute = Math.floor(remainder/1000/60%60); //分 var second = Math.floor(remainder/1000%60); //天 var str = `还有${day}天${hour}时${minute}分${second}秒`; var date = document.getElementById('date'); date.innerHTML = str; setTimeout(countDone,1000) } countDone()
利用两个日期对象,相减得到时间间隔的毫秒数,然后进行加工处理。
前端问题总结 文章被收录于专栏
总结一些前端常见的面试笔试题,来和大家分享鸭