我的代码工作正常,只不过它同时打开了所有链接。我想延迟一下。
这将同时打开所有(多个功能“打开”):
waitForKeyElements ("input.submit[onclick*='Open']", clickOpenBtn);
但是我希望每个函数调用之间都需要延迟(
clickOpenBtn
)。我完整的代码段:
setTimeout(CheckForZero, 30000); // OR just call CheckForZero() if you don't need to defer until processing is complete
function CheckForZero() {
waitForKeyElements ("input.submit[onclick*='Open']", clickOpenBtn);
setTimeout(CheckForZero, 30000);
}
function clickOpenBtn (jNode) {
triggerMouseEvent (jNode[0], "click");
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
我能做什么?
最佳答案
在这种情况下,将节点推入FIFO队列并使用setInterval
而不是setTimeout
来处理队列。代码变为:
var nodesToClick = []; //-- This array will hold the FIFO queue.
waitForKeyElements ("input.submit[onclick*='Open']", loadNodeQueue);
function loadNodeQueue (jNode) {
nodesToClick.push (jNode[0]); //-- Add to end
}
var nodeClkInterval = setInterval (workNodeQueue, 30000);
function workNodeQueue () {
if (nodesToClick.length) {
var node = nodesToClick.shift (); //-- Remove from beginning
triggerMouseEvent (node, "click");
}
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}