英文:
Golang: Automatic Refresh of a HTTP Page
问题
我正在使用Go编程语言执行一个HTTP页面。GO中的函数如下所示:
func main(){
...
http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
t:=template.New("New template")
child_template := t.New("New child template")
_, _ = child_template.Parse(output) // output是省略代码中的内容
t, err = t.ParseFiles("HTML_template.html")
_ = t.ExecuteTemplate(w, "HTML_template.html", output)
}
}
如何使/Page自动刷新?我尝试了以下方法,但没有成功。
func main(){
...
http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
for{
t:=template.New("New template")
child_template := t.New("New child template")
_, _ = child_template.Parse(output) // output是省略代码中的内容
t, err = t.ParseFiles("HTML_template.html")
_ = t.ExecuteTemplate(w, "HTML_template.html", output)
time.Sleep(time.Millisecond*100)
}
}
}
我正在尝试制作一个动态图表,用于绘制每秒钟传入数据的数量。如果我不断刷新浏览器,坐标轴也会重新加载,看起来很丑。HTML_template.html的内容如下:
<script type="text/javascript">
function plot(){
...
var data = [{{template "New child template"}}];
...
}
setInterval(func(){plot()},500);
</script>
英文:
I have a HTTP page getting executed using Go programming language. The function in GO looks like this:
func main(){
...
http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
t:=template.New("New template")
child_template := t.New("New child template")
_, _ = child_template.Parse(output) // output is from the omitted code
t, err = t.ParseFiles("HTML_template.html")
_ = t.ExecuteTemplate(w, "HTML_template.html", output)
}
}
How do I make /Page refreshes by itself? I have tried the following, but it doesn't work.
func main(){
...
http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
for{
t:=template.New("New template")
child_template := t.New("New child template")
_, _ = child_template.Parse(output) // output is from the omitted code
t, err = t.ParseFiles("HTML_template.html")
_ = t.ExecuteTemplate(w, "HTML_template.html", output)
time.Sleep(time.Millisecond*100)
}
}
}
I am trying to make a dynamic graph that plots the number of incoming data per second. If I keep making the browser refreshes, the axis will get reloaded too and it looks ugly. The HTML_template.html looks like this
<script type = "text/javascript">
function plot(){
...
var data = [{{template "New child template"}}];
...
}
setInterval(func(){plot()},500);
</script>
答案1
得分: 6
这可以通过简单地添加refresh元标签来实现,而无需使用Go、JavaScript、AJAX、SSE或Websockets。
将以下代码添加到页面的<head>
标签中:
<meta http-equiv="refresh" content="3" />
这将使页面每3秒刷新一次。
英文:
This can be done without Go, JavaScript, AJAX, SSE or Websockets by simply adding the refresh meta tag. Adding
<meta http-equiv="refresh" content="3" />
into your page's <head>
will cause it to refresh every 3 seconds.
答案2
得分: 1
你正在以错误的方式进行操作,你应该使用Websockets Gorilla 或者 go.net,或者至少使用ajax,但是重新加载整个页面非常低效。
英文:
You're going at this the wrong way, you should use Websockets Gorilla's or go.net's or at the very least use ajax, but reloading the whole page is very inefficient.
答案3
得分: -1
你真的真的真的需要使用WebSockets来完成这个任务,但是有一个快速而简单的方法。
在页面中添加一点jQuery代码:
if (window.location.href.indexOf('reload')==-1) {
window.location.replace(window.location.href+'?reload');
}
但是你真的需要使用WebSockets。
英文:
Your really really really need to use websockets to do this but there is a quick and dirty method.
Add a little jquery to the page:
if (window.location.href.indexOf('reload')==-1) {
window.location.replace(window.location.href+'?reload');
}
But you really need to use websockets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论