zhangxinxu / quiz

小测答题收集区
536 stars 43 forks source link

JS基础测试48期 #77

Open zhangxinxu opened 4 years ago

zhangxinxu commented 4 years ago

请实现:

在每年的5月20日13时14分,在浏览器的控制台输出'happy dog!'

看看谁的方法最简洁。


大家提交回答的时候,注意缩进距离,起始位置从左边缘开始;另外,github自带代码高亮,所以请使用下面示意的格式。

```js
// 你的JS代码写在这里
 ```

本期小测会以要点形式进行回复。

liyongleihf2006 commented 4 years ago
function print() {
  var _5201314,
    now = Date.now(),
    year = new Date().getFullYear()
  while (true) {
    _5201314 = new Date(year, 04, 20, 13, 14)
    if (_5201314 < now) {
      year += 1
    } else {
      break
    }
  }
  var dur = _5201314.getTime() - now
  setTimeout(() => {
    console.log('happy dog!')
    print()
  }, dur);
}
livetune commented 4 years ago
function checkDate() {
  const now = new Date();
  const specialDate = new Date(now.getFullYear() + "/05/20 13:14");
  specialDate - now < 2147483647 &&
    setTimeout(() => {
      console.log("happy dog!");
    }, specialDate - now);
}
checkDate();
zy017 commented 4 years ago
function logOnDate(date = '05/20 13:14', log = 'happy dog!') {
  var now = Date.now()
  var year = new Date().getFullYear()
  var date = new Date(`${year}/${date}`).getTime()
  date = date >= now ? date : new Date(`${year + 1}/${date}`).getTime()
  var delay = date - now
  // 由于延时(delay)大于 2147483647 (大约24.8 天)时就会溢出,导致定时器将会被立即执行
  if (delay > 2147483647) {
    setTimeout(() => {
      logOnDate()
    }, 2147483647)
  } else {
    setTimeout(() => {
      console.log(log)
    }, delay)
  }
}
logOnDate()
Seasonley commented 4 years ago

延时时间长了误差肯定大,我觉得没必要算时间差

var timer=function(){
    let t1=new Date(new Date().getFullYear(),4,20,13,14).getTime();
    Math.abs(Date.now()-t1)<1e3&&console.log('happy dog!');
    setTimeout(timer,1e3);
}
timer();
zer0fire commented 4 years ago

思路是每天轮询检查一次,符合 5.20 这一天就在 13:14 打印。有个问题是必须在 13:14 前运行这个函数,否则...就检查到下一天去了...

function timer () {
    let now = Date.now()
    let year = new Date().getFullYear()
    let month = new Date().getMonth()
    let day = new Date().getDate()
    let date520 = new Date(`${year}/05/20 13:14`).getTime()
    setTimeout(() => {
      if(day === 20 && month === 4) {
        setTimeout(() => {
          console.log('happy dog!')
        }, date520 - now)
        timer()
      } else {
        timer()
      }
    }, 86400000)

  }
  timer()
guqianfeng commented 4 years ago
setTimeout(() => {console.log('happy dog')},new Date('2021-05-20 13:14').getTime() - new Date().getTime())
xxf1996 commented 4 years ago
let time = null // 定时器句柄

function check520 () {
  const cur = new Date()
  const year = cur.getFullYear()
  let target = new Date(`${year}/5/20 13:14`)
  if (cur.getTime() > target.getTime()) { // 判断当前时间是否已经过了当年520
    target = new Date(`${year + 1}/5/20 13:14`) // 超过了等下一年吧
  }
  time = setTimeout(() => {
    console.log('happy dog!') // 开启嘲讽😊
  }, target.getTime() - cur.getTime()) // 等待时间差再执行
}

window.addEventListener('unload', () => {
  time && clearTimeout(time) // 页面卸载后清除定时器
})

check520()
ziven27 commented 4 years ago
(function () {
    var timer;
    function run520(params) {
        if ( new Date().setFullYear(1900) - new Date(1900, 4, 20, 13, 14) === 0 ) {
            console.log("happy dog!");
            // 只显示一次
            clearInterval(timer);
        }
    }
    run520();
    timer = setInterval(run520, 1000);
})();