i need make delays in loop, every time after amount of data (after few cycles/iterations through loop) sent server.
sending data , receiving respond server works fine, requested delays in loop still don't work.
thanks lot help.
edit: code changed, please check third answer (mine).
<!doctype html> <html> <body> <h2>ajax</h2> <button type="button" onclick="loaddoc()">request data</button> <p id="demo"></p> <script> function loaddoc() { var xhttp = []; var code = [ "woicel0q9p", "zjts4gyjej", "hjpmqocx31", "mp26n0bh01", "7tjnyzirjr", "z5middg4n2", "bx6mkyk0o7", "kvfvh1esqx", "40ady3zbe5", "v4nt360jr5", "fdi8afl680", "zh89n59xqr", "m6os2ox38h", "d8o76ydlm0", "86gbmjlixy", "1qrfvu26vk", "hfui9qv6dy", "vn83ogr825", "ddmpcbx2mf", "2m3qfpi234" ]; var = code.length; var j = code.length; var k = 5000; var p = 0; while (i--) { var process = (function(i) { if (p == 5) { p = 0; function func(i) { xhttp[i] = new xmlhttprequest(); xhttp[i].onreadystatechange = function() { if (xhttp[i].readystate == 4 && xhttp[i].status == 200) { if (i == j) { document.getelementbyid("demo").innerhtml = code[i] + ": " + xhttp[i].responsetext; } else { document.getelementbyid("demo").innerhtml += "<br><br>" + code[i] + ": " + xhttp[i].responsetext; } } }; xhttp[i].open("post", "https://www.example.com/services/postdata.svc", true); xhttp[i].setrequestheader("host", "www.example.com"); xhttp[i].setrequestheader("accept", "application/json, text/javascript"); xhttp[i].setrequestheader("accept-language", "cs,en-us;q=0.7,en;q=0.3"); xhttp[i].setrequestheader("accept-encoding", "gzip, deflate, br"); xhttp[i].setrequestheader("content-type", "application/json; charset=utf-8"); xhttp[i].setrequestheader("cache-control", "no-cache"); xhttp[i].setrequestheader("x-requested-with", "xmlhttprequest"); xhttp[i].setrequestheader("referer", "https://www.example.com/postdata-test.htm"); xhttp[i].setrequestheader("content-length", "37"); xhttp[i].setrequestheader("connection", "keep-alive"); xhttp[i].send('{"code":"'+code[i]+'","confirm":false}'); //console.log('hello - test if delay here'); p++; } settimeout(func(i), k); k += 5000; } else { xhttp[i] = new xmlhttprequest(); xhttp[i].onreadystatechange = function() { if (xhttp[i].readystate == 4 && xhttp[i].status == 200) { if (i == j) { document.getelementbyid("demo").innerhtml = code[i] + ": " + xhttp[i].responsetext; } else { document.getelementbyid("demo").innerhtml += "<br><br>" + code[i] + ": " + xhttp[i].responsetext; } } }; xhttp[i].open("post", "https://www.example.com/services/postdata.svc", true); xhttp[i].setrequestheader("host", "www.example.com"); xhttp[i].setrequestheader("accept", "application/json, text/javascript"); xhttp[i].setrequestheader("accept-language", "cs,en-us;q=0.7,en;q=0.3"); xhttp[i].setrequestheader("accept-encoding", "gzip, deflate, br"); xhttp[i].setrequestheader("content-type", "application/json; charset=utf-8"); xhttp[i].setrequestheader("cache-control", "no-cache"); xhttp[i].setrequestheader("x-requested-with", "xmlhttprequest"); xhttp[i].setrequestheader("referer", "https://www.example.com/postdata-test.htm"); xhttp[i].setrequestheader("content-length", "37"); xhttp[i].setrequestheader("connection", "keep-alive"); xhttp[i].send('{"code":"'+code[i]+'","confirm":false}'); p++; } })(i); } } </script> </body> </html>
you continuously spawning multiple calls process()
in while
, telling process
wait 5 seconds before callback happens.
// run loop on , on again while (true) { // create function called process process data var process = (function () { // data console.log("something"); // wait few seconds , again settimeout(process, 5000); // () right here says call process right }()); }
Comments
Post a Comment