feat: resetTimerOnUpdate + onUpdate回调 + queue可取消
This commit is contained in:
@@ -43,6 +43,10 @@ export const DEFAULTS = Object.freeze({
|
||||
onShow: null,
|
||||
onClose: null,
|
||||
onClick: null,
|
||||
onUpdate: null,
|
||||
|
||||
// 高级
|
||||
resetTimerOnUpdate: false,
|
||||
|
||||
// 国际化
|
||||
locale: 'zh-CN',
|
||||
|
||||
+20
-3
@@ -446,6 +446,18 @@ class Toast {
|
||||
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);
|
||||
return this;
|
||||
}
|
||||
@@ -977,16 +989,17 @@ const meToast = {
|
||||
queue(messages, opts = {}) {
|
||||
if (!Array.isArray(messages)) {
|
||||
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;
|
||||
const delay = opts.delay || 1000;
|
||||
const userOnClose = opts.onClose;
|
||||
|
||||
const showNext = () => {
|
||||
if (index >= messages.length) {
|
||||
if (cancelled || index >= messages.length) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
@@ -1017,6 +1030,10 @@ const meToast = {
|
||||
|
||||
showNext();
|
||||
});
|
||||
return {
|
||||
then: (fn, rj) => promise.then(fn, rj),
|
||||
cancel: () => { cancelled = true; },
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user