2017-08-26 35 views
-2

我是新角度2.我想在app.component中使用setTimout。我嘗試了很多其他帖子,但沒有爲我工作。Angular 2:SetTimout

下面是我正在嘗試的代碼。請以示例幫助我。

catchError(){ 
 
    if(navigator.onLine){ 
 
       console.log("connected"); 
 
       this.displayNetworkError = false; 
 
      } 
 
      else{ 
 
       console.log("Disconnected"); 
 
       this.displayNetworkError = true; 
 
      } 
 
}; 
 

 

 
internetConnection(){ 
 
     setTimeout(this.catchError(), 2000); 
 
    };

+0

'的setTimeout(() => this.catchError(),2000)';你傳遞了一些* callable *,而不是* *。 – jonrsharpe

回答

0

的setTimeout需要一個函數並以毫秒爲單位的持續時間。通常在JavaScript中,你會傳遞一個像setTimeout(function(){},1000)的函數。但是在打字稿中,你可以使用lambda表達式,例如setTimeout(()=> {},1000)

+0

可以請你幫我舉例如果你有 – Jay

+0

你叫做lambda的叫做「箭頭函數」,它們可以用新版本的純javascript,而不僅僅是打字稿。 –

+0

我正在嘗試這個方法,但它只能調用一次 – Jay

0

你正在執行功能catchError寫this.catchError()時

的setTimeout的第一個參數必須是一個函數 因此就使用this.catchError代替this.catchError()