On change client script is not working fine

anshul_jain25
Kilo Guru

Hi

I have written a code for onchange client script,

I want to populate different options for topic when differrent options are selected from category.

Every thing is working fine, its showing all the values correct, but after few seconds

for eg i have selected benefits in category and fastly i am selecting topic than its not showing any option, but when i have selected category and after 2-3 secs if i am selecting topic than i can see all the option,

So i believe this is some performance issue that my code is taking more time to populate option in topic field, you can see in below screenshots, first no options are coming , in second all option are coming when i selected topic after 2-3 secs

find_real_file.pngfind_real_file.png

Below is the code which i have written for this

I am getting a list from my table, using script include, than populating through add option function

now here '--None--' option is coming as soon as i am changing category value which is not in for loop, so i believe for loop is taking a bit of time to perform action

What should i do here, so that my code run fast.

find_real_file.png

1 ACCEPTED SOLUTION

satoshi_yamamur
Kilo Expert

Hi Anshul,


If you want to use the script include, use getXMLWait() instead of getXML().


getXML() is asynchronous so the function 'DoSomething()' runs after you get data from ServiceNow. Getting data takes 2-3 seconds.



http://wiki.servicenow.com/index.php?title=GlideAjax#Synchronous_Glide_Ajax


View solution in original post

15 REPLIES 15

Anshul,



I don't think setTimeout() works for this situation as it is also asynchronous function.


Also pausing script gets user experience worse. Users cannot do anything while receiving data.



Another workaround is to show 'Loading...' message in the choice field to user to let them know what is happening now.


Here is a sample (Client Script).



getTopic.getXML(DoSomething);


//Show Loading message and make the field read only


g_form.clearOptions('u_string_3');


g_form.addOption('u_string_3', 'Loading...', 'Loading...');


g_form.setReadOnly('u_string_3', true);


}



function DoSomething(resoponse) {


//When data is ready, remove Loading message


g_form.removeOption('u_string_3', 'Loading...');


g_form.setReadOnly('u_string_3', false);


g_form.addOption('u_string_3', '', '-- None --');



var topics_list ...