Skip to content

Instantly share code, notes, and snippets.

@zubayerahamed
Created February 17, 2018 05:17
Show Gist options
  • Save zubayerahamed/1c8ca6da26c3c857dd4fc8ee2ffa60e6 to your computer and use it in GitHub Desktop.
Save zubayerahamed/1c8ca6da26c3c857dd4fc8ee2ffa60e6 to your computer and use it in GitHub Desktop.
Create a Timer event using jquery and ajax
package com.coderslab;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@GetMapping
public String homePage() {
return "index";
}
@GetMapping("/fire")
public @ResponseBody String timerFireRequest() {
logger.info("Fire");
return "success";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"/>
<title>Countdown Timer</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="col-md-12">
<div id="display"></div>
<div id="message"></div>
</div>
<script type="text/javascript">
$(document).ready(function(){
var currentOpenTime = new Date();
setInterval(function() {
var now = new Date;
var h = now.getHours();
if(10 > h){
h = "0" + h;
}
var m = now.getMinutes();
if(10 > m){
m = "0" + m;
}
var s = now.getSeconds();
if(10 > s){
s = "0" + s;
}
if(s == currentOpenTime.getSeconds()){
$.get("/fire", {}, function(response){
console.log(response);
$('#message').html(response);
});
}
$('#display').text(h+" : "+m+" : "+s);
}, 1000)
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment