Jquery 如何使用单击功能停止while循环?
这有点复杂:我有一个表单,下面有一个文件输入字段和一个上传按钮。通过点击上传按钮,将启动一个函数,该函数执行ajax调用,以检查上传目录中是否已经存在同名文件。如果文件不存在,“开始上传”-功能启动:Jquery 如何使用单击功能停止while循环?,jquery,ajax,upload,Jquery,Ajax,Upload,这有点复杂:我有一个表单,下面有一个文件输入字段和一个上传按钮。通过点击上传按钮,将启动一个函数,该函数执行ajax调用,以检查上传目录中是否已经存在同名文件。如果文件不存在,“开始上传”-功能启动: function start_upload(){ while(start < SIZE) { //start and end is 0 and ~200Bytes - that's the size of the parts of the file i++;
function start_upload(){
while(start < SIZE) { //start and end is 0 and ~200Bytes - that's the size of the parts of the file
i++;
upload(blob.slice(start, end), i, name, SIZE);
start = end;
end = start + BYTES_PER_CHUNK;
}
}
一切正常,但由于可用性的原因,我也想创建一个取消上传按钮。我尝试了以下几点:
此取消功能停止所有当前的“POST”请求,但从零开始重新启动
$("#cancel").click(function(){
xhr.abort();
});
$( document ).ajaxStop(function() {
//do something after all ajax requests are finished or aborted
});
你知道我如何永久中止上传吗?你可以使用一个标志值,如下所示:
int flag=0;
function start_upload(){
while(start < SIZE && flag==0) { //start and end is 0 and ~200Bytes - that's the size of the parts of the file
i++;
upload(blob.slice(start, end), i, name, SIZE);
start = end;
end = start + BYTES_PER_CHUNK;
}
}
}))
希望它对您有用。是的,谢谢!:)嗯,我不得不使用两个#cancel click函数,它最终按预期工作,但现在一切都好了。两个函数,一个在函数外部,用于更改上载函数外部的全局标志变量(使用上载函数内部的函数将始终返回0),另一个在上载函数内部的#cancel函数使用xhr.abort();因为函数外部的xhr是未定义的。
int flag=0;
function start_upload(){
while(start < SIZE && flag==0) { //start and end is 0 and ~200Bytes - that's the size of the parts of the file
i++;
upload(blob.slice(start, end), i, name, SIZE);
start = end;
end = start + BYTES_PER_CHUNK;
}
}
$("#cancel").click(function(){
flag=1;