You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SweetAlert2计时器结束后,如何触发确认按钮同款Ajax请求?

解决SweetAlert2计时器结束触发确认逻辑的问题

要实现计时器结束时和点击确认按钮完全相同的效果,不能直接修改Swal.isConfirmed属性——因为then回调里的result对象是SweetAlert2内部生成的,手动修改全局属性不会改变它。以下是两种可行的解决方案:

方案1:主动触发确认按钮点击(推荐)

当计时器剩余时间归零时,调用Swal.clickConfirm()主动触发确认操作,这样弹窗会以「确认」的方式关闭,then回调中的result.isConfirmed会自动变为true,直接执行原有AJAX逻辑。

修改后的完整代码:

Swal.fire({
    title: 'Дали сакате да ги зачувате промените?',
    showDenyButton: true,
    confirmButtonText: 'Зачувај',
    confirmButtonColor: '#00CA4E',
    denyButtonText: `Откажи`,
    denyButtonColor: '#FF605C',
    html: '<strong></strong> секунди.<br/>',
    timer: 3000,
    didOpen: () => {
        timerInterval = setInterval(() => {
            const remainingTime = (Swal.getTimerLeft() / 1000).toFixed(0);
            Swal.getHtmlContainer().querySelector('strong').textContent = remainingTime;
            // 剩余时间为0时,主动触发确认按钮点击
            if (remainingTime <= 0) {
                clearInterval(timerInterval);
                Swal.clickConfirm();
            }
        }, 100);
    },
    willClose: () => {
        clearInterval(timerInterval);
    }
}).then((result) => {
    if (result.isConfirmed) {
        console.log('confirmed');
        $.ajax({
            type: "POST",
            url: "@Url.Action("SubstitutionAddPlayers","Scoreboard")",
            data: {
                jsonSubstitution: substitution,
            },
            success: function(data) {
                // 处理成功逻辑
            },
            error: function(req, status, error) {
                console.log(error);
            }
        });
    } else if (result.isDenied) {
        // 处理取消逻辑
    }
});

方案2:复用确认逻辑(兼容更多场景)

如果不想主动触发按钮点击,可以把AJAX逻辑抽成独立函数,在then回调中同时判断「确认按钮点击」和「计时器关闭」两种情况,直接调用该函数:

// 抽离确认逻辑为独立函数
function handleConfirm() {
    console.log('confirmed');
    $.ajax({
        type: "POST",
        url: "@Url.Action("SubstitutionAddPlayers","Scoreboard")",
        data: {
            jsonSubstitution: substitution,
        },
        success: function(data) {
            // 处理成功逻辑
        },
        error: function(req, status, error) {
            console.log(error);
        }
    });
}

Swal.fire({
    title: 'Дали сакате да ги зачувате промените?',
    showDenyButton: true,
    confirmButtonText: 'Зачувај',
    confirmButtonColor: '#00CA4E',
    denyButtonText: `Откажи`,
    denyButtonColor: '#FF605C',
    html: '<strong></strong> секунди.<br/>',
    timer: 3000,
    didOpen: () => {
        timerInterval = setInterval(() => {
            Swal.getHtmlContainer().querySelector('strong').textContent = (Swal.getTimerLeft() / 1000).toFixed(0);
        }, 100);
    },
    willClose: () => {
        clearInterval(timerInterval);
    }
}).then((result) => {
    // 兼容确认按钮点击和计时器关闭两种触发条件
    if (result.isConfirmed || result.dismiss === Swal.DismissReason.timer) {
        handleConfirm();
    } else if (result.isDenied) {
        // 处理取消逻辑
    }
});

关键说明

  • 方案1完全贴合你的需求,让计时器结束后和手动点击确认按钮的流程完全一致,result.isConfirmed会自然为true。
  • 方案2适合需要复用确认逻辑的场景,无需修改弹窗的关闭方式,直接在回调中兼容两种触发条件。

内容的提问来源于stack exchange,提问作者norman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:36:18