Ajinkya
Ajinkya

Reputation: 22720

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?

Upvotes: 0

Views: 206

Answers (1)

Rob W
Rob W

Reputation: 349222

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

Upvotes: 1

Related Questions