English 中文(简体)
脚本只运行一次
原标题:Script only runs once
  • 时间:2012-05-26 13:55:58
  •  标签:
  • javascript

这是一个脚本, [应该] 刷新页面, 找到价格, 如果价格低于用户指定的价格, 购买它。 如果价格更高, 它会刷新页面, 并在找到价格之前再次刷新它 :

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();
                }
            }
        }
    }
}

出于某种原因,它只运行一次,然后停止。有什么建议/帮助吗?

Thanks, Alex

问题回答

当您刷新时, 您的代码已不存在了 。 您有一个新页面, 有新代码 。 如果您了解这一点, 很明显不存在代码将无法运行 。

即使用户刷新浏览器, 也尝试使您的代码永久存在。 此外, 再检查您的代码, 它可能会有一些逻辑错误( 您可能在 if- else 部分有错误的牙套 ) 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签