简体   繁体   English

setTimeout似乎被循环忽略

[英]setTimeout seems to be ignored by loop

So basically I have a set of codes that will find all of my categories. 所以基本上我有一组代码可以找到我的所有类别。 It will click on them and get everything under it as well. 它将单击它们,并将所有内容都包含在其中。

    var Category = [];

var ID1;
var ID2;
var Level2CatsLen


var dispatchMouseEvent = function(target, var_args) {
    var e = document.createEvent("MouseEvents");
    e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
    target.dispatchEvent(e);
}

var Level1Cats = document.getElementsByClassName("p-pstctgry-lnk-ctgry "); //GETTING LEVEL 1 CATS

var Level1CatsLen = Level1Cats.length; //GETTING LEVEL 1 CAT LEN

    function GoToLevel2(i) { //GO TO NEXT LEVEL!
    setTimeout(function() {
    dispatchMouseEvent(Level1Cats[i], "mouseover", true, true);
    dispatchMouseEvent(Level1Cats[i], "click", true, true);
    }, 3000);
    }


    function GetLevel2() { //GET NEXT LEVEL
    setTimeout(function() {    
    Level2Cats = document.getElementsByClassName("p-pstctgry-lnk-ctgry");
    console.log("Level1CatLen: "+Level1CatsLen);
    console.log("Level2CatLen: "+Level2CatsLen);
    var Level2CatsLen = Level2Cats.length }, 3000);

    };


for (i = 0; i <= Level1CatsLen-1; i++) {
console.log(Level1CatsLen);
    var ID1 = Level1Cats[i].id;
    var temp1 = Level1Cats[i].innerHTML;
    temp1.replace(/&amp;/gi, "&").replace(/<[^>]*>/gi, "");

    setTimeout(function() {GoToLevel2(i)}, 10000);
    setTimeout(function() {GetLevel2()},20000); //RUN IT WITH TIMING


    // END OF LEVEL 1
    var extracats2 = Level2CatsLen - Level1CatsLen;
    if (extracats2 !== 2 || extracats2 !== 0) {
        for (ii = 0; ii < extracats2; ii++) { //LEVEL 2
        ID2 = Level2Cats[ii+Level1CatsLen].id;
        var temp2 = Level2Cats[ii+Level1CatsLen].innerHTML;
        temp2.replace(/&amp;/, "&").replace(/<[^>]*>/gi, "");

var Level2Children = []; 
for (l = 0; l < level1CatsLen; l++) {
    Level2Children.push(Level2Cats[l].id);
} 

//DO SOMETHING WITH CATEGORIES - Level 1

Category.push({Name: temp1, ID: ID2, ParentID: 'null', ChildrenIDs: Level2Children});

//FINISH
        }}}

I have allocated setTimeouts twice just to play with stuff. 我分配了两次setTimeouts只是为了玩玩。 But the issue I have is GoToLevel2 executes and then get category comes along and tries to get it straight away and naturally can't find anything. 但是我遇到的问题是执行GoToLevel2,然后出现get类别,并试图将其立即获取,自然找不到任何东西。 Though the setTimeout does not seem to have an effect. 尽管setTimeout似乎没有作用。 It slows the initial one and then after that is spams the console really quick for the amount of Level1Cats that are there. 它减慢了最初的速度,然后在垃圾邮件发出之后,控制台很快就提高了其中的Level1Cat数量。 Any idea why? 知道为什么吗?

Your variable i is global and is getting overridden more than you think probably. 您的变量i是全局变量,并且超出了您的想象。 As well it is being being passed to GoToLevel2() later and with a different value than you think. 同样,它也将稍后传递给GoToLevel2() ,并且其值不同于您的想象。 I would change that first and see if the problem still exists. 我将首先更改它,看看问题是否仍然存在。

function GoToLevel2(n) { //GO TO NEXT LEVEL!
  setTimeout(function() {
    dispatchMouseEvent(Level1Cats[n], "mouseover", true, true);
    dispatchMouseEvent(Level1Cats[n], "click", true, true);
  }, 3000);
}

// ...

for (var i = 0; i <= Level1CatsLen-1; i++) {
  // ...
  setTimeout(function() {GoToLevel2(i)}, 10000);
  // ...
}

Notice the var in the for loop, this limits the scope of i to the for -loop. 注意varfor循环,这限制的范围ifor -loop。 As well I changed it to n in GoToLevel2(n) to prevent confusion for the reader (should not be necessary for the browser). 同样,我在GoToLevel2(n)其更改为n ,以防止对读者造成混淆(浏览器不必要)。

You are using loop counter ('i' var) in setTimeout , and when the code triggers after timeout value of i has already incremented. 您正在setTimeout中使用循环计数器(' setTimeout ,并且在i超时值已经增加之后触发代码时。 Following is the example of using setTimeout in loops. 以下是在循环中使用setTimeout的示例。

for (var i=0; i < 10; i++)
{
    (
        function(i)
        {
            setTimeout(
                function()
                {
                    alert(i);
                }, 50
            );
        }
    )(i);
}

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

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