質問

Okay so the script works but setInterval keeps crashing my browser after 30secs or so. At least I think that's what's happening.

Here's the code:

var count = 0;

setInterval(function()
{
    var ifLose = document.getElementById('result-text').innerHTML;

    if (ifLose.length > 4) 
    {

        count++;
        document.getElementById("bet-multiplier").click();
        document.getElementById("bet-bt").click();

    }
    else 
    {
        for (count > 0; count--;) 
        {

        document.getElementById("bet-divider").click();

        }

        document.getElementById("bet-bt").click();

    }   
}, 1000);

What am I doing wrong? Does it have something to do with count and the for loop? Any help is greatly appreciated!

役に立ちましたか?

解決

This line is your problem (or at least a big part of the problem):

    for (count > 0; count--;) 

After the first time the timer handler runs, "count" will be -1. This loop will just keep going for a really long time.

You probably want

    for (; count > 0; count--)

The for loop header has three parts: initialization, test, and ... uhh, the third part, which is traditionally where you do iteration control but which really is just a place to put an expression. Your original code had the test expression first. That's not syntactically incorrect, because JavaScript just expects an expression, and a comparison expression like that is fine as far as that goes. It doesn't really do anything, however. The test expression in your version was count--, which is also syntactically OK and not a bad thing for a test expression. However, because the interval handler never resets "count" to 0, once it's decremented it to -1 the loop will keep going.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top