Hi I am currently doing a web application where my Java code will constantly fetch data from a database and update its own static variable. I can confirm that the variable is being updated constantly when I output it in the console, however when I want to use this variable inside my Javascript function (which is inside my .jsp code), it always takes the initial value and never gets updated despite being inside a SetInterval function.
The following is the Javascript segment where I access the Java static variable.
function moveMarker(map,marker){
setInterval( function(){
document.write(<%=DbManager.latitude%>);
},5000);
}
The DbManager.latitude variable is always in it's default value despite being changed consistently when the web app is running. I look forward to any answers I can get to fix this issue, or perhaps alternatives to what I'm trying to achieve.
Better you can goahead with AJAX for this.
<script type="text/javascript">
$(document).ready(function() {
setInterval(ajaxCall, 5000); // 5 MS
});
function ajaxCall() {
$.ajax({
type: "POST",
url: "/getUpdate/" ,
success: function(result) {
document.write(result);
}
}
});
</script>
Call your servlet/controller from AJAX and get the updated value
@Controller
@RequestMapping(value = "clause")
public class ClauseController {
@RequestMapping(value="getUpdate")
@ResponseBody
public String selectClause(ModelMap model) {
DbManager dbManager = DAO.getDbManager;
return "dbManager.latitude";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.