多くの関数やコールバックなどを備えた非常に大きなJSスクリプトがあります...私の最初のアクションの1つは、Ajax呼び出しから値を取得することです。次に、この値をスクリプトの開始時に定義されたグローバル変数に設定して、値を何度も参照できるようにします。たとえば、値を使用してユーザー言語を決定します。
// define my global var at the top of my script..
var globalOpCo = "";
// I then try to give this a value in the next function I call...
$.ajax({
url:"getURL",
type:"POST",
dataType:"json",
success:function(data){
if(data === null){
// do something...
}else{
// set the current language...
globalOpCo = data.Opco.toLowerCase();
console.log(globalOpCo); // this is the value I want it to be from data.Opco.toLowerCase()
// now do something....
}
},
error:function(xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(xhr.statusText);
console.log(thrownError);
}
});
globalOpCo
スクリプトの後半で、を次のような別の関数に渡したいと思います...
$("#aButton").on("click", function(){
anotherFunction(globalOpCo); // I want to pass the globalOpCo as an arguement but its value isn't get updated above?
});
ただし、の値はglobalOpCo
空の文字列です。#aButtonは、上記のajax呼び出しが実行される前または実行されるまでクリックできません。誰かが助けることができますか?