primefaces keyup或其他ajax事件延迟 [英] primefaces keyup or other ajax event delay

查看:100
本文介绍了primefaces keyup或其他ajax事件延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西:

<p:inputText...>
    <p:ajax event="keyup" update="somefield" listener="#{someBean.doSomething}"/>
</p:inputText>

但是我不想在每次按键时都发出Ajax请求,我想在用户停止书写后半秒钟执行该请求.

But I don't want to do an Ajax request on every keypress, I would like to do the request a half second after the user stops writing.

我在另一个问题中已经看到jQuery解决了这个问题: 如何延迟.keyup()处理函数,直到用户停止键入?

I have seen this problem solved in jQuery in another question: How to delay the .keyup() handler until the user stops typing?

我想知道是否有可能在素面上执行此操作,或者如何根据jQuery问题调整解决方案.
我正在使用PrimeFaces 3.0.M4.
预先谢谢你.

I would like to know if it's possible to do this on primefaces or how adapt the solution from the jQuery question.
I'm using PrimeFaces 3.0.M4.
Thank you in advance.

推荐答案

为什么不使用PrimeFaces的 RemoteCommand 组件?

Why don't you use PrimeFaces' RemoteCommand component?

它为您提供了一个全局Javascript函数,您可以随时随地调用它.而且,它可以让您调用Managed-Bean方法,并且它具有update属性用于部分更新.

It gives you a global Javascript function, that you can call from wherever whenever you want. And it lets you call the managed-bean method and it has the update attribute for partial update.

<p:inputText id="input" />

<h:form>
    <p:remoteCommand name="sendAjaxical" update="somefield" action="#{someBean.doSomething}"/>
</h:form>

注册事件处理程序,我从您发布的相同答案中借用了以下内容:

Register event handler, I borrowed the following from the same answer you posted:

var delay = (function() {
    var timer = 0;
    return function(callback, ms) {
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    };
})();


jQuery("#input").keyup(function() {
    delay(function() { sendAjaxical(); }, 2000); //sendAjaxical is the name of remote-command
});

这篇关于primefaces keyup或其他ajax事件延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆