Skip to content

Instantly share code, notes, and snippets.

@rebaomi
Forked from anonymous/cc.js
Created January 14, 2014 13:54
Show Gist options
  • Save rebaomi/8418603 to your computer and use it in GitHub Desktop.
Save rebaomi/8418603 to your computer and use it in GitHub Desktop.
cc.ajax = function (obj) {
var callback; // 非null表示外部已经调用了done操作。
var result; // 非null表示AJAX操作已经结束。
setTimeout(function () {
$.ajax(obj).done(function (res) { // AJAX操作完成时
if (callback) { // 外面已经给了callback?
callback(res); // 那么直接调用
} else { // 否则
result = res; // 保留结果,等待外部done操作。
}
});
}, 100);
return {
done: function (cb) { // 外部done操作调用时
if (result) { // 假如已经有了结果
cb(result); // 那么直接调用done
} else { // 否则
callback = cb; // 保留callback,等待AJAX操作结束
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment