Alex
Alex

Reputation: 1055

Script only runs once

This is a script that [should] refresh a page, find the price, and if the price is lower than what the user specifies, buys it. If the price is higher, it refreshes the page and does it again until the price is found:

var tags = document.getElementsByTagName('b');
var price;
var lp;
var sp;
var setSnipe = false;
var loaded = false;
for (i = 0; i < tags.length; i++) {
    console.log(tags[i].innerHTML);
    if( tags[i].innerHTML.indexOf('R$') !== -1) {
        if (i==5){
            var price = tags[i].innerHTML.match(/\d+/)[0];
            setSnipe = confirm("Lowest price: R$" + price +". Set a sniper?");
        } else {
        }
    } else {
    }
}
if (setSnipe == true) {
    lp = prompt("Snipe Price?");
    refresh();
}

function refresh() {
    location.reload(true);
    setTimeout('checkSnipe()', 3000);
}

function checkSnipe() {
    var tagss = document.getElementsByTagName('b');
    for (i = 0; i < 6; i++) {
        console.log(tagss[i].innerHTML);
        if( tagss[i].innerHTML.indexOf('R$') !== -1) {
            if (i==5){
                var price = tagss[i].innerHTML.match(/\d+/)[0];
                price = Number(price);
                if (price<=lp){
                    alert("Snipe Found!");
                } else {
                    alert("No snipe");
                    refresh();
                }
            }
        }
    }
}

For some reason, it only runs once then stops. Any advice/help?

Thanks, Alex

Upvotes: 1

Views: 708

Answers (2)

Ingila Ejaz
Ingila Ejaz

Reputation: 399

Try to make your code persist even if the user refreshes the browser. Plus, check your code again, it might have some logicl error (you might have misplaced braces in the if-else part).

Upvotes: 0

Amadan
Amadan

Reputation: 198496

When you refresh, your code does not exist any more. You have a new page, with new code. When you understand this, it becomes obvious that nonexistent code won't run.

Upvotes: 2

Related Questions