如何自动刷新页面的一部分 [英] How to auto refresh a section of a page

查看:262
本文介绍了如何自动刷新页面的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面的一部分,其中包含实时数据,我想每隔几分钟刷新一次页面,但由于页面的其他元素,这是不正确的。

I have a part of a page that has live data in it, I was thinking of just refreshing the page every couple of minutes, but that isn't right because of other elements of the page.

我该怎么办?我可以用什么语言来做这件事,什么是容易做什么,什么不做,什么做得好,什么做不好。也许是一些清晰的教程甚至是代码示例。

How can I do it? What language can I use to do this, what's easy to do and what isn't, what works well and what doesn't. Maybe some clear tutorials or even code examples.

我会像PHP这样的东西来做这个,但是不知道从哪里开始,从一点开始研究我发现Javascript和Ajax似乎是这方面的标准,但我对这些语言的了解还不尽如人意。

I'd live to do this in something like PHP, but don't have a clue where to start, from a bit of research i see that Javascript and Ajax seem to be the standard for this but my knowledge of these languages aren't up to scratch.

感谢您抽出宝贵时间和帮助人们。

Thanks for the time and help people.

哦,如果有任何帮助,显示的数据来自数据库。

Oh, the data that is being displayed is coming from a database if that's any help.

谢谢再次。

推荐答案

您可以使用jQuery这样的Javascript库,并执行以下简化示例:

You could use a Javascript library such as jQuery and do something like the following simplified example:

$("document").ready(function(){
  var interval = setInterval(refresh_box(), 60000);
  function refresh_box() {
    $("#myDiv").load('path/to/databasepage.php');
  }
} /*<= The closer ) bracket is missing in this line*/

并在路径/ /中databasepage.p hp 页面,您可以选择查询并回显结果。

and in your path/to/databasepage.php page you can have your select query and echo the results out.

这样做会设置超时以调用函数(在此case, refresh_box())每60秒(60,000毫秒)并将数据从 path /加载到/ databasepage.php 进入您的div,其ID为 myDiv

What this does is sets a timeout to call a function (in this case, refresh_box() ) every 60 seconds (which is 60,000 miliseconds) and loads the data from path/to/databasepage.php in to your div with the id myDiv.

编辑

如果要停止自动刷新,可以通过执行以下操作绑定元素以清除间隔:

If you want to stop the auto-refresh you can bind an element to clear the interval by doing the following:

// assuming you have a clickable element 
// (typically a button or so) id called 'stop_refresh'
$("#stop_refresh").click(function(){
  clearInterval(interval);
}

这篇关于如何自动刷新页面的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆