代码之家  ›  专栏  ›  技术社区  ›  Adeel Imran

使用&&而不是if语句调用函数

  •  0
  • Adeel Imran  · 技术社区  · 6 年前

    我有以下声明

     if (!this.customMsg) {
         this.customMsg = setInterval(() => {
             // doSomething();   
         }, 2000);
     }
    

    this.customMsg = !this.customMsg && setInterval(() => {
       // doSomething();   
    }, 2000);
    

    1 回复  |  直到 6 年前
        1
  •  4
  •   brk    6 年前

    你可以试试这个

    this.customMsg || (this.customMsg = setInterval(function() {
    }, 2000));