为什么 if(now-end )不是写成 if(now-end>0)?

来源:4-2 创建倒计时模块

西岚Silan

2018-04-14

class Timer {
    /**
     * 
     * 
     * @param {any} end 截至时间
     * @param {any} update 时间更新的回掉
     * @param {any} handle 倒计时结束后的回掉
     * @memberof Timer
     */
    countdown(end, update, handle) {
        const now = new Date().getTime();
        const self = this;
        if (now - end) { //如果当前时间大于截至时间
            handle.call(self);
        } else {
            let last_time = end - now; //倒计时所剩余时间
            const px_d = 1000 * 60 * 60 * 24;
            const px_h = 1000 * 60 * 60;
            const px_m = 1000 * 60;
            const px_s = 1000;
            let d = Math.floor(last_time / px_d); //剩余多少天
            let h = Math.floor((last_time - d * px_d) / px_h);
            let m = Math.floor((last_time - d * px_d - h * px_h) / px_m);
            let s = Math.floor((last_time - d * px_d - h * px_h - m * px_m) / px_s);
            let r = [];
            if (d > 0) {
                r.push(`<em>${d}</em>天`);
            }
            if (r.length || (h > 0)) {
                r.push(`<em>${h}</em>时`)
            }
            if (r.length || (m > 0)) {
                r.push(`<em>${m}</em>分`)
            }
            if (r.length || (s > 0)) {
                r.push(`<em>${s}</em>秒`)
            }
            self.last_time = r.join("");
            update.call(self, r.join(""));
            setTimeout(() => {
                self.countdown(end, update, handle);
            }, 1000);
        }

    }
}

我不太明白这个。。。不是一般都要判断他们的差是否大于0吗,如果不做大于0的判断的话,他们的差是负数,不是也是为true吗

写回答

1回答

快乐动起来呀

2018-04-15

应该是后者

0
0

ES6零基础教学 解析彩票项目

ES6从零开始,量身设计的迷你案例,让你全面掌握ES6

2579 学习 · 881 问题

查看课程