누군가이 코드를 머리에 쓰도록 나를 도울 수 있습니까? 나는이 코드에서 어떤 코드가 첫 번째로 실행될 것인가를 스스로 결정할 수 없다 :/분명히 이것은 항상 콘솔에 1을 프린트하도록 보장하지만, 왜 그런지는 알 수 없다. 또한 함께 선은 "orig_fn.bind.apply ..."나를 위해 매우 혼란 :/함수를 항상 비동기로 만듭니다.
function asyncify(fn) {
var orig_fn = fn,
intv = setTimeout(function(){
intv = null;
if (fn) fn();
}, 0)
;
fn = null;
return function() {
// firing too quickly, before `intv` timer has fired to
// indicate async turn has passed?
if (intv) {
fn = orig_fn.bind.apply(
orig_fn,
// add the wrapper's `this` to the `bind(..)`
// call parameters, as well as currying any
// passed in parameters
[this].concat([].slice.call(arguments))
);
}
// already async
else {
// invoke original function
orig_fn.apply(this, arguments);
}
};
}
function result(data) {
console.log(a);
}
var a = 0;
ajax("..pre-cached-url..", asyncify(result));
a++;
내가 [바인딩] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), [적용] (HTTPS에 대한 설명서를 읽어 시작할 것이다 : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply),의 setTimeout (https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout) 등 –
또한 디버거를 가져 와서 코드를 단계별로 실행하십시오. 그것이 "가장 먼저 실행될 코드"를 보는 가장 쉬운 방법입니다. –
디버거는 비동기 호출을 조사하려고 할 때 비참하게 실패 할 것입니다. 그것은 할 수 있지만, 크롬 개발 도구를 사용하여 특정 숙달이 필요합니다. –