Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set up the duration of info message once so that we dont have to write time out at each time

ChanchalG
Tera Expert

We have a requirement where they want to automatically close info message(g_form.addInfoMessage) after 30 sec. In order to do that i have to find and write time out to each info message in system. 

I am looking for is there anyway we can define it globally at some place so where ever the info message is written it will take 30 sec to  time out only?

1 ACCEPTED SOLUTION

itsTushar
Kilo Guru

Yes, you can create a client script that automatically detects and closes all info messages after 30 seconds without modifying each individual message. Here's the most effective approach:

function onLoad() {
    // Override addInfoMessage to mark auto-close messages
    var originalAddInfoMessage = g_form.addInfoMessage;
    g_form.addInfoMessage = function(message, persist) {
        var msgElement = originalAddInfoMessage.call(this, message, persist);
        if (!persist) {
            msgElement.dataset.autoclose = 'true';
        }
        return msgElement;
    };

    // Check for messages every second
    setInterval(function() {
        var messages = document.querySelectorAll('[data-autoclose="true"]');
        messages.forEach(function(msg) {
            if (!msg.dataset.closeTime) {
                msg.dataset.closeTime = Date.now() + 30000;
            } else if (Date.now() > parseInt(msg.dataset.closeTime)) {
                $(msg).fadeOut(500, function() {
                    $(this).remove();
                });
            }
        });
    }, 1000);
}

 

View solution in original post

5 REPLIES 5

Will this work for catalog client scripts as well ?