2015-04-29 56 views
4

我已經玩了一點與SweetAlert插件:Sweet alert甜警報定時器 - 完成功能

我想作一個刪除按鈕,其中前實際刪除該用戶被提示。然後,當用戶再次按下「刪除」時,然後說「完成」,並且用戶必須再次點擊「確定」以使提示消失。

SweetAlert有一個定時器功能,所以你可以在幾秒鐘左右自動關閉最後一個「完成」提示,這很好。他們也有一個功能,在這裏你可以實現一個功能,當用戶在「完成」提示中點擊「確定」時就可以運行該功能。問題是,如果在定時器完成後提示自動關閉,那麼該函數不會運行。

任何想法如何做到這一點?

當定時器和功能沒有運行:

swal({ 
    title: "Deleted!", 
    text: "Your row has been deleted.", 
    type: "success", 
    timer: 3000 
    }, 
    function() { 
      location.reload(true); 
      tr.hide(); 
    }); 

沒有計時器,但與工作功能(在點擊「確定」按鈕):

swal("Deleted!", "Your row has been deleted.", "success"), function() { 
         location.reload(); 
         tr.hide(); 
        }; 

回答

7

說明

我認爲你必須從功能採取swal分開。我的意思是顯示swal,該功能在後臺運行,模式自動關閉。

使用Javascript/jQuery的:

swal({ 
    title: "Deleted!", 
    text: "Your row has been deleted.", 
    type: "success", 
    timer: 3000 
    }); 
    function() { 
     location.reload(true); 
     tr.hide(); 
    }; 

你的代碼SweetAlert的例子:

swal({ 
    title: "Are you sure?", 
    text: "You will not be able to recover this imaginary file!", 
    type: "warning", 
    showCancelButton: true, 
    confirmButtonColor: "#DD6B55", 
    confirmButtonText: "Yes, delete it!", 
    cancelButtonText: "No, cancel plx!", 
    closeOnConfirm: false, 
    closeOnCancel: false 
    }, 
    function (isConfirm) { 
     if (isConfirm) { 
      swal({ 
       title: "Deleted!", 
       text: "Your row has been deleted.", 
       type: "success", 
       timer: 3000 
      }); 
      function() { 
       location.reload(true); 
       tr.hide(); 
      }; 
     } 
     else { 
      swal("Cancelled", "Your imaginary file is safe :)", "error"); 
     } 
    }); 
+0

這是不正確的...... – iamchriswick

1

我找到了解決辦法

目前我正在使用sweetAlert進行實驗,並找到了解決方案。
這是我的自定義函數,用於創建sweetalert,它會在幾秒鐘的計時器後關閉。

var sweetAlert = function(title, message, status, timer = 5000, isReload = false){ 
    swal({ 
     title : title, 
     text : message + '<br/>This pop up will close automatically in <strong class="swal-timer-count">' + timer/1000 + '</strong> seconds...', 
     type : status, 
     html : true, 
     timer : timer, 
     allowEscapeKey : false 
    }, function() { 
     swal.close(); 
     if(isReload) 
      location.reload(true); 
    }); 
    var e = $(".sweet-alert").find(".swal-timer-count"); 
    var n = +e.text(); 
    setInterval(function(){ 
     n > 1 && e.text (--n); 
    }, 1000); 
} 

可以調用使用此代碼
記住,定時器使用milisecond這種方法。

sweetAlert('Congratulation!', 'You successfully copy paste this code', 'success', 3000, false); 
+0

尼斯!!!!我一直在尋找這個小時,你知道這很簡單!非常感謝你!! – Evis