中止ajax调用

Abort an ajax call?

本文关键字:调用 ajax 中止      更新时间:2023-09-26

My currentReq var设置为回调方法:

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

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

我知道我可以使用:

this.currentReq.abort();

但是如果currentReq不是ajax调用呢?我如何测试这一点,并且只有在它是ajax调用时才中止?当我尝试中止一个标准延迟时,我遇到了一个错误。

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

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

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

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