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

查看:17
本文介绍了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?

我想知道是否可以在 primefaces 上执行此操作,或者如何根据 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 函数,您可以随时随地调用该函数.它允许您调用托管 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天全站免登陆