AJAX リクエストの実行中に同期 JavaScript を偽造しようとしています。getPagePath(id)
ページIDを指定してページのページパスを取得する必要がある関数があり、Web APIを介してデータを受け取ります。サーバーに ajax リクエストを送信し、ページ パスを受け取るだけです。しかし、何が起こっているのですか: ページ パスを要求すると、コードが実行され続け、空の var が返されます。その後、ajax 呼び出しは終了しますが、遅くなります。
私の説明はあまり意味がないことを知っているので、ここに私のコードがあります:
var getPagePath = function() {
// Function to check if this.pagePath is set.
var pagePathReady = function() {
console.log('PAGEPATH: CHECKING');
if (this.pagePath && this.pagePath != null) {
return true;
} else {
return false;
}
};
if (!pagePathReady()) {
// No pagePath defined so lets set it.
this._setPagePath();
while (!pagePathReady())
{
// Not yet defined, check again..
// *** The problem ***
// This while loop is running insanely fast making the browser crash.
// How can I make this wile loop pause for 1 sec?
// *******************
console.log('PAGEPATH: NOT READY -> CHECK AGAIN');
}
// READY
console.log('PAGEPATH: READY -> VALUE: ' + this.pagePath);
return this.pagePath;
} else {
return this.pagePath;
}
};
var _setPagePath = function() {
if (!this.pagePathRequestFired) {
this.pagePathRequestFired = true;
// Fire request.
system.url(
this.getNodeId(),
function(url) {
// Request ready, set pagePath.
this.pagePath = url;
this.pagePathRequestFired = false;
},
this
);
} else {
// Call already running..
}
};
より説明的なコメントに問題を設定しました。
前もって感謝します!