带有remoteCommand的Progressbar setInterval

时间:2014-12-05 09:49:00

标签: javascript jquery jsf primefaces progress-bar

我有以下远程命令:

<p:remoteCommand id="usedCall"
                 name="queryUsed"
                 oncomplete="fetchUsedMemory(#{performanceProvider.queryUsedMemory()})"
                 process="@all" />

它调用bean函数并将结果传递给脚本块中定义的fetchUsedMemory JS函数。我想每隔2秒执行一次remoteCommand,直到取消并在ProgressBar上显示结果。我尝试了以下方法:

function fetchUsedMemory(usedMemory) {
    var maxValue = jQuery('#TotalMemory').val();

    var pbClient = PF('pbClient');
    var newValue = usedMemory;
    var percentg = (newValue / maxValue) * 100;

    pbClient.setValue(percentg);

    jQuery('#progressText').val('Max: ' + maxValue + ' NEW: ' + newValue + ' %: ' + percentg);
}

此函数会更新进度条和inputText以验证结果。然后是启动整个过程的函数:

function start() {
    PF('startButton1').disable();
    PF('cancelButton1').enable();
    window['progress'] = setInterval(queryUsed, 1000);
}

我试图在这里传递remoteCommand的名称,但它只被称为一次。没有进一步打电话给豆。最后,这是我的HTML:

    <p:panel id="mProgressPanel">

        <p:progressBar id="progressBar" widgetVar="pbClient" style="width: 500px;" />
        <p:inputText id="progressText" style="display: block; width: 1000px;" />

        <f:facet name="footer">
            <p:commandButton id="StartProgress" disabled="true"
                             type="button" onclick="start()"
                             value="Start"
                             widgetVar="startButton1" />
            <p:commandButton id="StopProgress" disabled="true"
                             type="button" onblur="cancel()"
                             value="Stop"
                             widgetVar="cancelButton1"/>
        </f:facet>
    </p:panel>

有没有办法让它工作,以便setInterval正确调用remoteCommand?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用<p:poll>,这是一个想法

XHTML

<p:poll interval="2" listener="#{performanceProvider.queryUsedMemory()}" 
        oncomplete ="updateMemory(xhr, status, args)" widgetVar="pollWV"
        process="@this" />

ManagedBean

public void queryUsedMemory() {
    RequestContext.getCurrentInstance().addCallbackParam("memory", getUsedMemory() + 10);
}

的javascript

function updateMemory(xhr, status, args) {
   if(args.memory) {
      //update what ever you want

      //Stop polling if the memory is 100
      if(args.memory === 100) {
         PF('pollWV').stop();
      }
   }
}