Greasemonkey 延迟... setTimeout 不起作用
我一直在玩一个网站,我想在其中每隔 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,请使用
setInterval()
。一种方法:
或者,不使用全局变量:
Use
setInterval()
for this.One way:
Alternatively, without using global vars:
只是为了解释为什么您的代码不起作用:您立即调用
.click
方法(将()
在函数名称调用该函数之后),并实际将该函数的返回值传递给setTimeout
。for
循环速度非常快,似乎所有事情都同时发生。您必须传递函数引用给
setTimeout
,例如匿名函数: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 tosetTimeout
. Thefor
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: