简体   繁体   中英

Escape some characters in JavaScript

I have an ajax call in my legacy application:

i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(
    "CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI
);

I want to add this Ajax request in setTimeout method. To escape " I added \\ . I came up with following line:

setTimeout("i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId(\"CRC:QueryStatus\", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI)",50000);

Now I am not getting any error on console but Ajax call is also not working either.

Am I missing anything?

The rec and/or scopedCallbackQRSI variables are probably defined in a local scope (thus not accessible from the global scope). When setTimeout is called with a stringified function as a first argument, the function is executed within the scope of window .

To maintain the scope (and be able to use the local variables), wrap your code in a function, and pass it as a first argument to setTimeout :

setTimeout(function(){
    i2b2.CRC.ajax.getQueryResultInstanceList_fromQueryResultInstanceId("CRC:QueryStatus", {qr_key_value: rec.QRS_ID}, scopedCallbackQRSI);
}, 50000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM