[英]Javascript 'while' loop to load single page App does not work
I am trying to load a single page app until I have certain number of Items.我正在尝试加载单页应用程序,直到我有一定数量的项目。 My function passes the correct new scrollHeight but the code does not work.我的function传递了正确的新滚动高度,但代码不起作用。 Here is my code, I start it by calling scrollInitiate() function.这是我的代码,我通过调用scrollInitiate() function 来启动它。 What am I missing?我错过了什么?
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function scrollInitiate() {
while ($x('//*[@id="items"]/container').length < 200) {
console.log("attempting");
wait(5000);
scrollIt(document.scrollingElement.scrollHeight);
}
}
function scrollIt(height) {
console.log("scrolling to: " + height);
document.scrollingElement.scrollTo(0, height);
}
You can't use while
to wait in javascript, due to javascript blocking nature.由于 javascript 的阻塞性质,您不能在while
中等待。 Instead use setTimeout
:而是使用setTimeout
:
function scrollInitiate() {
if ($x('//*[@id="items"]/container').length < 200) {
console.log("attempting");
scrollIt(document.scrollingElement.scrollHeight);
setTimeout(scrollInitiate, 5000);
}
console.log("success");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.