简体   繁体   English

我怎么能在javascript中发表声明“for”等待

[英]how i can make statement “for” wait in javascript

I have 1 app using phone gap 2.5.0 wrote for ios. 我有一个应用程序使用手机差距2.5.0为ios写道。 I am try to download files in server to my app. 我尝试将服务器中的文件下载到我的应用程序。 I need downloads a lot files (84 files); 我需要下载很多文件(84个文件); and i use 1 loop for to download all. 我用1个循环来下载所有。

But when i use "for" statement, it's loop too fast and download all files almost at the same time, and some file can't download complete because timeout. 但是当我使用“for”语句时,它的循环速度太快并且几乎同时下载所有文件,并且由于超时,某些文件无法下载完成。

So I want 1 file download finish and then the next file begin download. 所以我想要1个文件下载完成,然后下一个文件开始下载。

How can I do that? 我怎样才能做到这一点?

Please help me! 请帮我! I am dumper... 我是自卸车......

This is my code: 这是我的代码:

var fileTransfer = new FileTransfer();

for (var i = 0; i < anhup.length; i++) {
    console.log("anhup[" + i + "]: " + anhup[i]);
    fileTransfer.download(
        "http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
                + anhup[i], window.rootFS.fullPath + "/" + anhup[i],
        function(entry) {
            sa = entry.fullPath;
            console.log("download complete: " + entry.fullPath);

        }, function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        });
}

Try 尝试

function download() {
    var i = 0;

    var fileTransfer = new FileTransfer();

    function doDownload(i) {
        fileTransfer.download(
                "http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
                        + anhup[i], window.rootFS.fullPath + "/" + anhup[i],
                function(entry) {
                    sa = entry.fullPath;
                    console.log("download complete: " + entry.fullPath);

                    if (i < anhup.length - 1) {
                        doDownload(i + 1);
                    }

                }, function(error) {
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code" + error.code);
                });
    }

    doDownload(0)
}

Make a function and keep calling itself until the object is empty, like so. 创建一个函数并继续调用自己,直到对象为空,就像这样。

function loadFile() {
    // no more to load
    if(!anhup.length)
        return;

    var context = anhup.shift();

    fileTransfer.download(url, function(entry) {
        console.log("download complete: " + entry.fullPath);

        loadFile();
    }, function(error) {
        // ...
    });
}

You can't do that with "for" loop, you need to start downloading the next file when 'success' callback function is called; 你不能用“for”循环来做,你需要在调用'success'回调函数时开始下载下一个文件; the easiest way is to make 'i' global variable, and rewrite your code like this (not tested!): 最简单的方法是制作'i'全局变量,并像这样重写你的代码(未经过测试!):

var i = 0;
var downloadNext;

var onSuccess = function(entry) {
    sa = entry.fullPath;
    console.log("download complete: " + entry.fullPath);
    i = i + 1;
    if (i < anhup.length) {
       downloadNext();
    }
};

var onFail = function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
};

downloadNext = function() {
    console.log("anhup[" + i + "]: " + anhup[i]);
    fileTransfer.download(
        "http://smartphone.thnt.vn/VietGames/GhepTranhTu/IOS/update/"
            + anhup[i], window.rootFS.fullPath + "/" + anhup[i], 
         onSuccess,
         onFail
    );
};

// start downloading the first one
downloadNext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在前一个函数完成之前进行语句? - How can I make a statement wait until a previous function is finished? 如何使JavaScript函数等待输入或值? - How can I make a JavaScript function wait for input or value? 如何让程序等待 javascript 中的按钮按下? - How can I make a program wait for a button press in javascript? 如何让程序等待 javascript 中的变量更改? - How can I make a program wait for a variable change in javascript? 如何让程序在继续使用javascript之前等待if语句完成? - How to make the program wait for the if statement to finish before continuing in javascript? 如何让我的 Google App Script 在电报机器人中等待答案,然后运行 if 语句? - How can I make my Google App Script wait for an answer in telegram bot then run the if statement? 如何在JavaScript中为if语句创建数组或变量? - How can I make an array or variable for an if statement in javascript? 如何制作连接到 HTML 的 JavaScript &#39;if&#39; 语句? - How can I make a JavaScript 'if' statement that connects to HTML? 如何让 Protractor 不等待 $timeout? - How can I make Protractor NOT wait for $timeout? 如何让我的代码等到我从服务器获得处理过的图像以在 Javascript 中显示 - How can I make my code wait until I got processed image from the server to display in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM