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

Ankur Bawiskar
Tera Patron
Tera Patron

@ChanchalG 

I don't think you can define it globally, but you will have to use script and give timeout

// Display the info message
g_form.addInfoMessage('This is my info message');

// Clear the info message after 30 seconds
setTimeout(function() {
g_form.clearMessages();
}, 30000); // 30000 milliseconds = 30 seconds

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@ChanchalG 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@ChanchalG 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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);
}