Benas
Benas

Reputation: 2332

Submit form continuously multiple times while user holds button pressed

How can one achieve multiple form submission while the user holds the button pressed.

<h:commandButton value="Multiple submission" action="#{mngr.doSomething}"/>

Basically I just want to keep invoking mngr.doSomething method as long as i hold the button pressed. Any ideas?

Upvotes: 1

Views: 437

Answers (2)

BalusC
BalusC

Reputation: 1108722

That's only possible if you use <f:ajax> whereby you do not render the button itself. You can then use onmousedown and onmouseup events to start and stop the rage submit by explicitly invoking the onclick() handler of the button and you can use <f:ajax onevent> to continue explicitly invoking the onclick() handler on successful return as long as the button appears to be still pressed.

Here's a kickoff example:

<h:form>
    <h:commandButton value="submit" action="#{bean.submit}" onmousedown="startRageSubmit(this)" onmouseup="stopRageSubmit(this)">
        <f:ajax onevent="processRageSubmit" />
    </h:commandButton>
</h:form>

(again, when using <f:ajax render>, make sure that the button is not covered by the selector)

with this JS:

function startRageSubmit(button) {
    button.pressed = true;
    button.onclick();
}

function processRageSubmit(data) {
    if (data.status == "success" && data.source.pressed) {
        data.source.onclick();
    }
}

function stopRageSubmit(button) {
    button.pressed = false;
}

Upvotes: 3

Soosh
Soosh

Reputation: 812

You can add javascript event listener with javascript setInterval() function to submit the form(with AJAX) in your desire intervals.

Upvotes: 1

Related Questions