feat: resetTimerOnUpdate + onUpdate回调 + queue可取消

This commit is contained in:
tianhao
2026-06-16 13:00:10 +08:00
parent 34417d0057
commit 25fb1b1d48
2 changed files with 24 additions and 3 deletions
+4
View File
@@ -43,6 +43,10 @@ export const DEFAULTS = Object.freeze({
onShow: null, onShow: null,
onClose: null, onClose: null,
onClick: null, onClick: null,
onUpdate: null,
// 高级
resetTimerOnUpdate: false,
// 国际化 // 国际化
locale: 'zh-CN', locale: 'zh-CN',
+20 -3
View File
@@ -446,6 +446,18 @@ class Toast {
content.innerHTML = `${safeTitle}${safeMessage}`; content.innerHTML = `${safeTitle}${safeMessage}`;
} }
// resetTimerOnUpdate: 更新内容后重置计时器
if (this.config.resetTimerOnUpdate && this.config.duration > 0) {
cancelAnimationFrame(this.rafId);
this.startedAt = Date.now();
this.remaining = this.config.duration;
this._startTimer();
}
if (typeof this.config.onUpdate === 'function') {
try { this.config.onUpdate(this); } catch (e) { console.error('onUpdate callback error:', e); }
}
Toast.trigger('afterUpdate', this); Toast.trigger('afterUpdate', this);
return this; return this;
} }
@@ -977,16 +989,17 @@ const meToast = {
queue(messages, opts = {}) { queue(messages, opts = {}) {
if (!Array.isArray(messages)) { if (!Array.isArray(messages)) {
console.error('MeToast.queue: messages must be an array'); console.error('MeToast.queue: messages must be an array');
return Promise.resolve(); return { then: (fn) => Promise.resolve().then(fn), cancel: () => {} };
} }
return new Promise((resolve) => { let cancelled = false;
const promise = new Promise((resolve) => {
let index = 0; let index = 0;
const delay = opts.delay || 1000; const delay = opts.delay || 1000;
const userOnClose = opts.onClose; const userOnClose = opts.onClose;
const showNext = () => { const showNext = () => {
if (index >= messages.length) { if (cancelled || index >= messages.length) {
resolve(); resolve();
return; return;
} }
@@ -1017,6 +1030,10 @@ const meToast = {
showNext(); showNext();
}); });
return {
then: (fn, rj) => promise.then(fn, rj),
cancel: () => { cancelled = true; },
};
}, },
/** /**