简体   繁体   English

如何在HTML中建立链接以在Java中启动计时器?

[英]How to make a link in html to start the timer in java?

I added the code bellow... So, basically, its a timer, that starts every 7 seconds to write something in the console... 我添加了下面的代码...因此,基本上,它是一个计时器,该计时器每7秒启动一次以在控制台中编写内容。

now i have a html page...from witch i want to make a link... and that link should start the timer... 现在我有一个html页面...来自女巫我想建立一个链接...并且该链接应该启动计时器...

@Resource
private TimerService timerService;

@PostConstruct
private void init() {
    timerService.createTimer(1000, 7000, "1");
}

the code above set the timer to 7 sec, and i pass the arg 1 to it...i need it later on... 上面的代码将计时器设置为7秒,然后我将arg 1传递给它...以后需要它...

// public Timer timer = timerService.createTimer(1000, 10000, "1");



@Timeout
@GET
@Path("/timerStart")
public void TimeOut(Timer timer) {

    /*
     * String ss = timer.getInfo().toString(); String brojS =
     * ss.substring(0, 1); ss.substring(1);
     */
    Long brojI = Long.parseLong(timer.getInfo().toString(), 10);
    // HttpServletRequest req = (HttpServletRequest) timer.getInfo().
    // //@Context HttpServletRequest req
    // execute(,brojI);

    System.out.println("Timer Service : " + timer.getInfo());
    System.out.println("Current Time : " + new Date());
    System.out.println("Next Timeout : " + timer.getNextTimeout());
    System.out.println("Time Remaining : " + timer.getTimeRemaining());
    System.out.println("____________________________________________");

}

the problem is, that once i run the server...the timer starts also...and thats a problem...i need him to start once i press the link on the html page... 问题是,一旦我运行服务器...计时器也将启动...那是一个问题...一旦我按html页面上的链接,我就需要他启动...

any suggestions? 有什么建议么?

Create an anchor tag and use onclick event to trigger an ajax 创建一个标记并使用onclick事件触发一个ajax

HTML HTML

<a href="" onclick="someFunction()"> Start Timer</a>

JS JS

somefunction(){
  // ajax request
}

I solved it: 我解决了:

This goes into the html page: 这进入html页面:

<a href="rest/dss/startTimers">Start Timer</a>

and this goes into the code: 这进入了代码:

@Resource
private TimerService timerService;

@GET
@Path("/startTimers")
public void init() {
    System.out.println("Started the timer!");
    timerService.createTimer(1000, 1000, "1");
}

@Timeout
public void execute(Timer timer) {
    System.out.println("Timer Service : " + scriptId);
    System.out.println("Current Time : " + new Date());
    System.out.println("Next Timeout : " + timer.getNextTimeout());
    System.out.println("Time Remaining : " + timer.getTimeRemaining());
    System.out.println("____________________________________________");

}

and btw, here is a code to stop the timer: 顺便说一句,这是停止计时器的代码:

@GET
@Path("/stopAllTimers")
public void StopTimer() {

    for (Timer timer : timerService.getAllTimers()) {
        System.out.println("All Timers Stopped");
        timer.cancel();
    }
}

and the link in the html page: 以及html页面中的链接:

<a href="rest/dss/stopAllTimers">Stop All Timers</a>

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

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