2016-09-27 78 views
0

我曾經通過警報彈出窗口顯示我的驗證以及成功/失敗消息,但現在我的要求是使用HTML標籤文本顯示相同的消息,並使它們在特定消息之後消失多少時間。我目前使用的超時功能:將警報更改爲標籤的最佳方法

if ($("#lblErrorMessage").text() != "") { 
    clrMsg = setTimeout(function(e) { 
     $("#lblErrorMessage").text(""); 
     clearTimeout(clrMsg); 
    }, 5000); 
} 

這種做法是非常亂,沒有辦法檢查報文是否成功(需要顯示更長的時間),或者需要顯示的錯誤/失敗消息(爲更短的時間)。任何人都可以提出一個可以在整個頁面中使用的功能,也可以滿足我想要的功能?

在此先感謝

回答

0

添加了一個類?

你不顯示如何添加消息#lblErrorMessage ...
但我想你也可以添加一個類,如「成功」或「失敗」。

$("#lblErrorMessage").text("Some success messsage to user.").addClass("success"); 
removeMessage(); 

$("#lblErrorMessage").text("Some error messsage to user.").addClass("fail"); 
removeMessage(); 

然後,這裏是新setTimeout函數:

function removeMessage(){ 
    if ($("#lblErrorMessage").text() != "") { 
     if $("#lblErrorMessage").hasClass("success"){ 
      clrDelay = 5000; 
     } 
     else if $("#lblErrorMessage").hasClass("fail"){ 
      clrDelay = 2500; 
     } 
     clrMsg = setTimeout(function(e) { 
      $("#lblErrorMessage").text(""); 
      //clearTimeout(clrMsg);  // No use for that. 
      $("#lblErrorMessage").removeClass("success fail"); // Remove classes for future messages. 
     }, clrDelay); 
    } 
} 
+0

謝謝。我會試試這個 – Abhay

相關問題