中止ajax电话?

时间:2015-06-22 21:22:45

标签: javascript jquery

我的currentReq var设置为回调方法:

this.currentReq = cb.apply(this,args);

稍后我可能希望中止该方法,如果它是ajax调用。

我知道我可以使用:

this.currentReq.abort();

但是如果currentReq不是ajax调用怎么办?我如何测试这个并且只有在ajax调用时才中止?当我尝试中止标准延期时,我收到错误。

2 个答案:

答案 0 :(得分:3)

您可以先测试一下abort方法是否存在:

if (typeof(this.currentReq.abort) == "function") {
     this.currentReq.abort();
}

答案 1 :(得分:2)

如果错误是未定义的方法,您应该能够测试函数本身是否存在

if(typeof this.currentReq.abort === 'function') {
    this.currentReq.abort();
}
相关问题