1

My code works fine, except it opens all links at the same time. I would like to use a delay.

This opens all (more than one function "Open") at the same time:

waitForKeyElements ("input.submit[onclick*='Open']", clickOpenBtn);

but I want a delay between each function call (clickOpenBtn).

My complete code snippet:

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


What can I do?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295

2 Answers2

1

You should be more specific about the selector used at waitForKeyElements, something like the parent node of all links. So that the actionFunction would execute once and then you could do:

waitForKeyElements ("...", function (p) {
    $("input.submit[onclick*='Open']", p).each(clickOpenBtn);
});

function clickOpenBtn (index, jNode) {
    setTimeout(function () {
        triggerMouseEvent (jNode[0], "click");
    }, 1000 * index);
}
w35l3y
  • 8,613
  • 3
  • 39
  • 51
1

In this case, push the nodes into a FIFO queue and use setInterval, not setTimeout to work the queue. The code becomes:

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);
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295