gpt4 book ai didi

jQuery - 停止函数

转载 作者:行者123 更新时间:2023-12-01 02:38:11 25 4
gpt4 key购买 nike

我想要实现的是,如果出现错误(我想出的错误)并且包含“错误”一词,则该函数将停止运行。该函数基本上由 5 个 ajax 请求组成,当一个请求成功时,另一个请求就会启动......就像这样......

function thing() {
$.ajax({
...
...
success:
function(html){
errorGet(html);
$.ajax({
...
...
success:
function(html){
errorGet(html);
...
...

我有一个函数“errorGet()”来尝试停止该函数;

function errorGet(data){
var re = /error/mgi;
if( data.search( re ) == 0 ){
thing.stop;
}
}

但我知道 thing.stop 不起作用,但有人知道如何停止该过程吗?

最佳答案

您可以拥有一个无需异步即可调用 AJAX 请求的函数。这样就不必进行网络连接,您只需 return 即可退出该函数。

function thing() {
var stop = false;

$.ajax({
async: false,
...
success:
function(html){
stop = errorGet(html);
}
}

if(stop) {
return false;
}

$.ajax({
async: false,
...
success:
function(html){
stop = errorGet(html);
}
}
}

function errorGet(data){
var re = /error/mgi;
if( data.search( re ) == 0 ){
return false; //no error
}
return true;
}

关于jQuery - 停止函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2222845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com