admin管理员组文章数量:1401431
I'm trying to add a sleep / delay function inside a js file, This one:
var webTest = function()
{
let regex = /^https?:\/\//;
let url = $('#list_urls').val().split('\n');
var xmlhttp = [], i;
var myObj2 = [], i;
for(let i = 0; i < url.length; i++)
{
(function(i) {
xmlhttp[i] = new XMLHttpRequest();
url[i] = url[i].replace(regex, '');
xmlhttp[i].open("GET", "=<MY_API_KEY>&url="+url[i], false);
xmlhttp[i].onreadystatechange = function() {
if (xmlhttp[i].readyState === 4 && xmlhttp[i].status === 200) {
myObj2 = JSON.parse(xmlhttp[i].responseText);
document.getElementById("demo"+i).innerHTML = myObj2.results[1].categories;
}
};
xmlhttp[i].send();
})(i);
console.log(`The Server2: `+ myObj2);
}
}
I want this script to pause for 10 second and then again do work and then again pause for 10 second and do like this untill text length is greater thatn i in loop! My code works if i run for single time but it doesn't works if i run in loop because the website has rate limit in the api so that's why i'm trying to add a sleep function.
So what i've tried is await sleep(); method and also tried setTimeout method but it's not working as expected in sort it doesn't works at all with my code!
await sleep(); just doesn't works at all and displays msg like Uncaught SyntaxError: await is only valid in async functions and async generators webTestfile.js:27
I'm trying to add a sleep / delay function inside a js file, This one:
var webTest = function()
{
let regex = /^https?:\/\//;
let url = $('#list_urls').val().split('\n');
var xmlhttp = [], i;
var myObj2 = [], i;
for(let i = 0; i < url.length; i++)
{
(function(i) {
xmlhttp[i] = new XMLHttpRequest();
url[i] = url[i].replace(regex, '');
xmlhttp[i].open("GET", "https://website./API?key=<MY_API_KEY>&url="+url[i], false);
xmlhttp[i].onreadystatechange = function() {
if (xmlhttp[i].readyState === 4 && xmlhttp[i].status === 200) {
myObj2 = JSON.parse(xmlhttp[i].responseText);
document.getElementById("demo"+i).innerHTML = myObj2.results[1].categories;
}
};
xmlhttp[i].send();
})(i);
console.log(`The Server2: `+ myObj2);
}
}
I want this script to pause for 10 second and then again do work and then again pause for 10 second and do like this untill text length is greater thatn i in loop! My code works if i run for single time but it doesn't works if i run in loop because the website has rate limit in the api so that's why i'm trying to add a sleep function.
So what i've tried is await sleep(); method and also tried setTimeout method but it's not working as expected in sort it doesn't works at all with my code!
await sleep(); just doesn't works at all and displays msg like Uncaught SyntaxError: await is only valid in async functions and async generators webTestfile.js:27
Share Improve this question edited Nov 24, 2020 at 22:41 Vicky Malhotra asked Nov 24, 2020 at 22:38 Vicky MalhotraVicky Malhotra 3601 gold badge5 silver badges17 bronze badges 3- 2 I want this script to pause for 10 second and then again do work and then again pause for 10 second! You cannot pause code. What you can do is run more code after a delay, but the currently running function on the call stack must plete before any new code can start. – Scott Marcus Commented Nov 24, 2020 at 22:40
- Yeah i know that javascript doesn't have any sleep functionality but what can be done here to achieve at least 10-11second delay? tried setTimeout method but nothing worked :/ – Vicky Malhotra Commented Nov 24, 2020 at 22:43
-
This answer shows a
sleep()
-alternative for JS in ES6. – Oskar Grosser Commented Nov 24, 2020 at 22:45
2 Answers
Reset to default 4You can make use of ES6's async/await
-feature!
To use await
, it needs to be in a function/expression body declared async
.
Basically, this will make your function be asynchronous, and make it wait for a Promise
to be fulfilled. We make that Promise be fulfilled after a set delay using setTimeout()
.
Note that "after a set delay" does not mean "exactly after", it basically means "as early as possible after".
By doing this, the asynchronous function waits for the promise to be fulfilled, freeing up the callstack in the meantime so that other code can be executed.
The order of execution of this example is (simplified) as follows:
sleepingFunc()
is placed on callstack- In iteration:
await
for Promise to be fulfilled, suspending this call本文标签: arraysAdd TimeoutSleep function inside javascript loopStack Overflow
版权声明:本文标题:arrays - Add TimeoutSleep function inside javascript loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744308166a2599900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
- In iteration:
发表评论