进度条设置间隔与远程命令

Progressbar setInterval with remoteCommand

本文关键字:程命令 命令 设置      更新时间:2023-09-26

我有以下远程命令:

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

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

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

此函数将更新进度条和输入文本以验证结果。然后启动整个过程的函数:

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

我试图在这里传递远程命令的名称,但它只被调用一次。没有对豆子进行进一步的呼叫。最后这是我的 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 正确调用远程命令?感谢您的帮助!

你可以使用<p:poll>,这里有一个想法

.xhtml

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

管理豆

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