实时刷新/更新
Real Time Refresh / Update
我有一个MVC 3项目,在服务器上发布。
例如,我有一个保存数据从(PC1)
到(PC2)
的函数。
用户在(PC2)
中看到的data(data in jqgrid)
可能是open(open in page)
, (PC1)
保存数据后会自动刷新或更新页面或jqgrid。
我的jqgrid版本是4.3.3
。
希望你们明白我在我的帖子里的意思。如果投票失败,发布反馈。谢谢。
您可能想使用ajax来完成这样的工作,请阅读下面
如果我理解你的意思,是你想轮询服务器实时更新的间隔或其他东西…
选项1
向服务器发出一个正常的无状态ajax调用,然后强制服务器在有限的时间内保持请求[以克服服务器开销]
这也可以称为反向ajax或comet。
除非你打算使用websocket技术,否则我不建议你这么做。
if(isset($_GET['finite'])){
#declare time for a session
$_SESSION['typing']=$reduce_browser_overhead=time();
#remember to close the session before entering the loop;
#if u dont close, then the browser will not reload the same website untill the connection is close or satisfied
session_write_close();
function loop(){
#do this to access external variables ===> $reduce_browser_overhead;
global $con,$reduce_browser_overhead;
#explicitly check 2exit
#please do this to release mysql connection since they are in a loop
if($reduce_browser_overhead+4<time()){
echo ' ';ob_flush();flush();
if(connection_aborted()){
#do some work here before you finally exit the connection
exit;
}$reduce_browser_overhead=time();
}
#php prepare statement...
$looper=$con->prepare("SELECT ROW FROM TABLE WHERE ID=SOMETHING AND $_SESSION[typing]=SOME_ROW");
#The statement above willcause the loop to work
#If a table had been update and table has not yet updated, this sql will detemin by the current time
#meaning that if the time[integer] of SOME_ROW is not equal to the time in the session variable,
#then it will let go to the client and then again it will continue looping untill the time in the
#SOME TABLE ROW changes....
$looper->execute();
$looper->store_result();
if($looper->num_rows>0){
sleep(2);
#do some work before looping again
loop();
#you have to explicitly return to this loop to work as expected.
return;
}else{
#send back data to the user or the client listening on the connection
session_start();$_SESSION['typing']=time();session_write_close();
#update the session before finishing the request so that the next time the request comes, the time will be equal to the DBserver time in the row and hence causing the loop again and again => more like a cycle
echo 'After some time the server has received new data which is =>> '.$newdata;
}
}loop();
exit;
//In another file on in the same document as the php / your server file ==> do javascript below
//first issue a normal / stateless ajax request to the target server
$.ajax({
//All optional but url required!
url:'abc.php?var_one=blabla',
cache:true,//whether to cache the requests
timeout:(1000*60)*20,//timeoutthe request
success:function(data){
//if the server successfully completed the request
//do some work here with data returned
},
error:function(){
//if the server return an error
//do more work around
//or call the function again
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
相关文章:
- 如何在不刷新页面的情况下更新显示框
- EXTJS-图表刷新,重绘-如何更新图表
- 如何检查Json文件更新,如果更新了,则用更新的数据刷新我的页面
- JQGrid使用服务器编辑后的更新数据刷新数据
- 实时更新,无需刷新页面
- ExtJS网格在存储更新后未刷新
- 在内容刷新或更新时调整iframe的大小
- 为什么我的 cshtml 页面的 javascript 没有在浏览器中更新/刷新
- HTML5 网址更新而不刷新
- 动态更新/刷新 KmlLayer
- Ember JS - 从路由操作更新/刷新模型数据
- yii2:使用 Pjax 更新/刷新/重新加载页面,与请求页面不同
- ExtJS 6 网格无法正确更新/刷新列标题
- 离子和角度 - 从控制器更新/刷新视图
- 更新/刷新JQuery移动滑块有效
- 只更新(刷新)PartialView提交后点击
- 使用站点更新刷新缓存
- ExtJS 4-更新/刷新单个记录
- 更新刷新函数中的挖空可观察字符串
- 如何在插入jQuery后更新/刷新DOM项