简体   繁体   English

如何在JSF中创建计时器计数器

[英]How to create a timer counter in JSF

I want to create an online test system, so i need a timer. 我想创建一个在线测试系统,所以我需要一个计时器。 When user start the test the timer will start counting down. 当用户开始测试时,计时器将开始倒计时。 But when user go to next question refresh the page the timer will still running. 但是,当用户转到下一个问题刷新页面时,计时器仍将运行。 How can I do that any suggestion? 我该怎么做呢?

You can use PrimeFaces Poll option. 您可以使用PrimeFaces Poll选项。

The counter will always be saved on the server. 计数器将始终保存在服务器上。

You can also use PrimeFaces Extensions, they have timer in their API. 您也可以使用PrimeFaces扩展程序,它们的API中包含计时器

Example: you put start of the test into @SessionScoped bean, then calculate seconds remaining which will be used as an input to <pe:timer> element: 示例:将测试的开始放入@SessionScoped bean中,然后计算剩余的秒数,该秒数将用作<pe:timer>元素的输入:

@SessionScoped
public class UserData {
     private Date testStart; 

     /* getters and setters for testStart */

     public int secondsRemaining() {
        //calculate seconds remaining until end of the test from this.testStart
        return secondsRemaining;
     }
}

and then your test.xhtml would contain element 然后您的test.xhtml将包含元素

Time remaining: <pe:timer format="HH:mm:ss"
                    timeout="#{userData.secondsRemaining()}"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM