简体   繁体   English

Javascript设置超时循环

[英]Javascript set timeout on a loop

I have a function that reads data from Web Sql and sends it to web service to insert data into SQL Server. 我有一个从Web Sql读取数据并将其发送到Web服务以将数据插入SQL Server的功能。 All would be nice and easy if not the fact that it takes about 40 seconds for the web service to process all the information. 如果不是这样,那么Web服务处理所有信息大约需要40秒钟的事实,一切都会变得轻松愉快。 I have set a timeout on sending data out to web service but it does not seems to be working....When I have more than one row in a table it sends out only the first row and skips the remaining ones, so it means that timeout does not work. 我已经设置了将数据发送到Web服务的超时时间,但是似乎无法正常工作....当我在表中有多个行时,它仅发送第一行并跳过其余的行,因此这意味着超时不起作用。 Any help appreciated. 任何帮助表示赞赏。

        db2.transaction(function (tx) {
        tx.executeSql('SELECT * FROM offlineCabinDefects', [], function (tx, results) {
            var len = results.rows.length, i;
            for (i = 0; i < len; i++) {

                var airline = results.rows.item(i).Airline;
                var tail = results.rows.item(i).Tail;
                var fn = results.rows.item(i).FlightNumber;
                var ad = results.rows.item(i).ActionDate;
                var ca = results.rows.item(i).CabinArea;
                var ci = results.rows.item(i).CabinItem;
                var uf = results.rows.item(i).UnserviceableFlag;
                var rn = results.rows.item(i).RowNumber;
                var sn = results.rows.item(i).SeatNumber;
                var dscr = results.rows.item(i).Description;
                var r = results.rows.item(i).Revision;
                var user = results.rows.item(i).UserName;
                var pass = results.rows.item(i).Password;

                data2 = "{'Airline':'" + airline +
                            "','Tail':'" + tail +
                            "','FlightNumber':'" + fn +
                            "','ActionDate':'" + ad +
                            "','CabinArea':'" + ca +
                            "','CabinItem':'" + ci +
                            "','UnserviceableFlag':'" + uf +
                            "','RowNumber':'" + rn +
                            "','SeatNumber':'" + sn +
                            "','Description':'" + dscr +
                            "','Revision':'" + r +
                            "','UserName':'" + user +
                            "','Password':'" + pass + "'}";


                 setTimeout(sendWS(data2), 50000);
            }
            dropTable2();
            document.getElementById("offlineresult").innerHTML = ('');
            document.getElementById("offlinetbl").innerHTML = ('');
        });
    });
}

Your function within the timeout is being executed immediately! 您的超时时间内的功能将立即执行! If you're passing params, wrap it in an anonymous function else it will be executed immediately! 如果要传递参数,请将其包装在匿名函数中,否则它将立即执行!

setTimeout(function() {
    sendWS(data2)
}, 50000);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM