簡體   English   中英

VSCode 擴展:如何在 x 時間后自動刪除錯誤消息?

[英]VSCode extension: How can I remove the error message automatically after x amount of time?

我正在構建一個 VSCode 擴展,如果我的條件為真,我會彈出一條錯誤消息。 我希望錯誤消息在例如 5 秒后消失。

我嘗試了以下方法:

let count = 0

if (condition) {
    setInterval(() => {
          count++
    }, 1000);
    while (count <= 5) {    
        vscode.window.showErrorMessage(`Error!`);
    }
}

但這沒有用......,我怎樣才能做到這一點?

謝謝

盡管有人認為用戶必須快速閱讀這樣的自動隱藏消息,但我仍然認為它對特定情況很有用。 前段時間 VS Code 有消息敬酒,非常適合這種消息,但它們已被刪除。 這就是為什么我使用進度 API 寫了一個替換:

/**
 * Shows a message that auto closes after a certain timeout. Since there's no API for this functionality the
 * progress output is used instead, which auto closes at 100%.
 * This means the function cannot (and should not) be used for warnings or errors. These types of message require
 * the user to really take note.
 *
 * @param message The message to show.
 * @param timeout The time in milliseconds after which the message should close (default 3secs).
 */
export const showMessageWithTimeout = (message: string, timeout = 3000): void => {
    void window.withProgress(
        {
            location: ProgressLocation.Notification,
            title: message,
            cancellable: false,
        },

        async (progress): Promise<void> => {
            await waitFor(timeout, () => { return false; });
            progress.report({ increment: 100 });
        },
    );
};

(取自https://github.com/mysql/mysql-shell-plugins/blob/master/gui/extension/src/utilities.ts

/**
 * Waits for a condition to become true.
 *
 * @param timeout The number of milliseconds to wait for the condition.
 * @param condition A function that checks if a condition has become true.
 *
 * @returns A promise that resolves to true, if the condition became true within the timeout range, otherwise false.
 */
export const waitFor = async (timeout: number, condition: () => boolean): Promise<boolean> => {
    while (!condition() && timeout > 0) {
        timeout -= 100;
        await sleep(100);
    }

    return timeout > 0 ? true : false;
};

(取自https://github.com/mysql/mysql-shell-plugins/blob/master/gui/frontend/src/utilities/helpers.ts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM