[英]execute jquery every 5 seconds withing a change event
im having a chart which will be updated every 5 seconds to show the how many users have entered a building and the user has an option to select how many users are on each floor. 我有一个图表,该图表每5秒更新一次,以显示进入建筑物的用户数量,并且用户可以选择每个楼层有多少用户。 this is also updated every 5 seconds
这也会每5秒更新一次
below is my code to get the users entered the building 下面是我让用户进入建筑物的代码
var interval = 5000;
function get_Users() {
var users= $.ajax({
type: 'POST',
url: '<?php echo base_url('index.php')?>/building/feed',
data: $(this).serialize(),
success: function (users) {
$('.current').html(users);
},
complete: function (users){
setTimeout(get_Users, interval);
}
});
}
setTimeout(doFeed, interval);
below is used to get the users on each floor 以下用于获取每个楼层的用户
$(document).on('change','.floor',function(){
$.ajax({
url: 'floor/chart',
type: 'post',
data: asset,
dataType: 'json',
success: function (users) {
$(".current").html(users.floor);
},
});
});
what i need to do is once its successfully changed, post and get the chart values every 5 seconds.. 我需要做的是一旦成功更改它,每5秒发布并获取图表值。
how can this be done? 如何才能做到这一点? any help will be appreciated.
任何帮助将不胜感激。
You could do that by create a named function for change
handler and trigger a timeout
function once the first one complete as you have done it for other function in your code. 您可以通过为
change
处理程序创建一个命名函数并在第一个函数完成后触发一个timeout
函数来完成此操作,就像对代码中的其他函数所做的一样。
$(document).on('change','.floor',changeFloorHandler);
function changeFloorHandler() {
$.ajax({
url: 'floor/chart',
type: 'post',
data: asset,
dataType: 'json',
success: function (users) {
$(".current").html(users.floor);
},
complete: function (users){
setTimeout(changeFloorHandler, interval);
}
});
}
Note: I would suggest you to have a common ajax
function in your code with custom parameters holding callback
functions reference for timeout
instead of multiple function definitions to avoid redundant code. 注意:我建议您在代码中使用公用的
ajax
函数,并使用自定义参数保存timeout
callback
函数引用,而不要使用多个函数定义,以避免冗余代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.