DIV CSS 佈局教程網

easyUI messager.alert 提示框自動關閉的實現
編輯:關於JavaScript     

easyUI 的 $.messager.alert 默認需要手動點擊才能關閉,有些場景(比如操作成功後的輕提示)希望它自己消失。思路很簡單:用 JS 的定時器到點觸發關閉。

參數說明

title:提示框的標題

msg:提示信息內容

ico:提示圖標類型,如 'info'、'warning'、'error'

函數實現

function alert_autoClose(title, msg, icon) {
  var interval;
  var time = 1000;   //計時粒度 1 秒
  var x = 2;         //倒計時 2 秒

  $.messager.alert(title, msg, icon, function () {});

  interval = setInterval(fun, time);

  function fun() {
    --x;
    if (x == 0) {
      clearInterval(interval);
      $(".messager-body").window('close');
    }
  }
}

要點在於最後一定要 clearInterval 把定時器清掉,否則它會一直空轉。另外 $(".messager-body").window('close') 是直接操作 dialog 組件的方法,如果同一頁面存在多個 messager 實例,建議加上更精確的選擇器,避免誤關其他彈層。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved