有没有办法退出函数,具体取决于GET请求的结果.
例如,在下面的函数中hi,如果GET结果在data哪里data === '1',我想退出该函数.
function hi () {
$.ajax({
url: "/shop/haveItem",
type: "GET",
success: function (data) {
if (data == '1') {
// exit hi() function
}
}
});
// some executable code when data is not '1'
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能完成这个?
我认为解决方案可以是这样的
function hi () {
$.ajax({
url: "/shop/haveItem",
type: "GET",
success: function (data) {
if (data == '1') {
ifData1();
} else {
ifDataNot1()
}
}
});
}
function ifData1 () { /* etc */ }
function ifDataNot1 () { /* etc */ }
Run Code Online (Sandbox Code Playgroud)
如果你有一个ajax函数,你应该总是使用回调函数.如果使ajax函数同步,则浏览器将在ajax调用期间被阻止.这意味着应用程序在呼叫期间将保持无响应.
| 归档时间: |
|
| 查看次数: |
12979 次 |
| 最近记录: |