Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何使用单击功能停止while循环?_Jquery_Ajax_Upload - Fatal编程技术网

Jquery 如何使用单击功能停止while循环?

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++;

这有点复杂:我有一个表单,下面有一个文件输入字段和一个上传按钮。通过点击上传按钮,将启动一个函数,该函数执行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++;
        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;