How to set delay in my on change client script

VIKAS MISHRA
Tera Contributor

Below is my the small part of code that i am using in my client script, could anyone please help to know that how i can place a delay there, i mean after setting the value in "category" i want to waiit for few seconds then only i should populate the sub categor.

 

basicall i have code "window.setTimeout(function, 5000)" to use for delay purpose but i do not know what i need to put in place keyword "function".

 

g_form.setValue('category',''+cat);
	               
g_form.setValue('subcategory', subCat);

 

6 REPLIES 6

Pavankumar_1
Mega Patron

Hi @VIKAS MISHRA ,

refer below question.

https://www.servicenow.com/community/developer-forum/need-to-hold-screen-for-3-secs-when-any-option-...

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

The url that you have provided i have already seen that but there the code is saying that it will hold for 5 second and then it will run the another function mentioned in the code "window.setTimeout(function, 5000)" but the issue is i do not know what should i put in place of keyword "Function" becuase after holiding the screens i do not need to run any other function, instead of that after holding the screen for few seconds i need to run the next line of code to set the sub category field , so in that case how should i using this delay method 

Hi @VIKAS MISHRA ,

If you place your script with in the function it will hold for 2 secs.

You have to place the code with in the function that you need to execute after 2 secs.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.setValue('category', '' + cat);
    var setTimer = '';
    setTimer = setTimeout(setTimerDelay, 2000); /*to set the timer for 2 sec*/
    function setTimerDelay() {
        alert('timer waits for 2 sec');
        g_form.setValue('subcategory', subCat);
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

jaheerhattiwale
Mega Sage
Mega Sage

@VIKAS MISHRA you can use the setTimeout like below

 

setTimeout(function(){

g_form.setValue('category',''+cat);                
g_form.setValue('subcategory', subCat);

}, 5000);

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023