Greasemonkey 延迟... setTimeout 不起作用

发布于 2024-11-09 10:42:29 字数 548 浏览 8 评论 0原文

我一直在玩一个网站,我想在其中每隔 interval 秒继续点击一个按钮 i 次。

我的代码是:

clickbidBtn1 = function() {
    var bidBtn=document.getElementById("BidButton");

    var interval = 15000;
    for (var i=3; i>=0; i--){
    setTimeout(bidBtn.click(1);,i*interval);
};

我发现 GM 同时执行所有 i 次点击,而不是按预期延迟。有没有办法延迟点击时间?假设我希望该函数每 15 秒单击一次按钮 i 次。

我正在考虑给它更多的变量,并在 settimeout 代码部分添加一个变量,该变量仅在单击时执行,然后在进入下一个 settimeout 之前将增加的变量与当前变量进行比较...但还没有想清楚...对于一个简单的过程来说这似乎是一个复杂的过程...:(我会稍微尝试一下

I've been playing around with a site, in which I want to continue clicking a button for i amount of times every interval seconds.

My code is:

clickbidBtn1 = function() {
    var bidBtn=document.getElementById("BidButton");

    var interval = 15000;
    for (var i=3; i>=0; i--){
    setTimeout(bidBtn.click(1);,i*interval);
};

I've found out that GM executes all i amount of clicks at the same time, not with the intended delay. is there a way to delay the time of click? Say i wanted the function to click the button every 15 second for i amount of times.

I was thinking of giving it some more variables, and adding one variable in the settimeout code part, which only executes @ the click, then comparing increased variables with current ones before going to the next settimeout... but haven't thought it through yet... it seems to be a complicated process for a simple process... :( i wll play around with it a bit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-11-16 10:42:29

为此,请使用 setInterval()

一种方法:

var bidClickTimer       = 0;
var numBidClicks        = 0;

function clickbidBtn1 ()
{
    var interval        = 15000;
    bidClickTimer       = setInterval (function() {BidClick (); }, interval);
}

function BidClick ()
{
    numBidClicks++;
    if (numBidClicks > 3)
    {
        clearInterval (bidClickTimer);
        bidClickTimer   = "";
    }
    else
    {
        bidBtn.click (1);
    }
}

clickbidBtn1 ();

或者,不使用全局变量:

function clickbidBtn1 ()
{
    var interval            = 15000;
    this.numBidClicks       = 0;
    this.bidClickTimer      = 0;
    this.BidClick           = function () {
                                numBidClicks++;
                                if (numBidClicks > 3)
                                {
                                    clearInterval (bidClickTimer);
                                    bidClickTimer   = "";
                                }
                                else
                                {
                                    bidBtn.click (1);
                                }
                            };
    this.bidClickTimer      = setInterval (function(thisScope) {thisScope.BidClick (); }, interval, this);
}

clickbidBtn1 ();

Use setInterval() for this.

One way:

var bidClickTimer       = 0;
var numBidClicks        = 0;

function clickbidBtn1 ()
{
    var interval        = 15000;
    bidClickTimer       = setInterval (function() {BidClick (); }, interval);
}

function BidClick ()
{
    numBidClicks++;
    if (numBidClicks > 3)
    {
        clearInterval (bidClickTimer);
        bidClickTimer   = "";
    }
    else
    {
        bidBtn.click (1);
    }
}

clickbidBtn1 ();

Alternatively, without using global vars:

function clickbidBtn1 ()
{
    var interval            = 15000;
    this.numBidClicks       = 0;
    this.bidClickTimer      = 0;
    this.BidClick           = function () {
                                numBidClicks++;
                                if (numBidClicks > 3)
                                {
                                    clearInterval (bidClickTimer);
                                    bidClickTimer   = "";
                                }
                                else
                                {
                                    bidBtn.click (1);
                                }
                            };
    this.bidClickTimer      = setInterval (function(thisScope) {thisScope.BidClick (); }, interval, this);
}

clickbidBtn1 ();
挖个坑埋了你 2024-11-16 10:42:29

只是为了解释为什么您的代码不起作用:您立即调用.click方法(将()在函数名称调用该函数之后),并实际将该函数的返回值传递给setTimeoutfor 循环速度非常快,似乎所有事情都同时发生。

您必须传递函数引用setTimeout,例如匿名函数:

setTimeout(function() {
    bidBtn.click(1);
}, i*interval);

Just to explain why your code does not work: You are calling the .click method immediately (putting () after a function name calls the function) and actually passing the return value of that function to setTimeout. The for loop is so fast that everything seem to happen at the same time.

You have to pass a function reference to setTimeout, e.g. an anonymous function:

setTimeout(function() {
    bidBtn.click(1);
}, i*interval);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文