自动刷新文档标题? [英] Auto Refresh document title?

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

问题描述

我尝试了很多方法让页面标题每3秒自动更新一次,这样标题就可以显示他们有多少条未读消息。

I have tried many ways to have the page title automatically update every 3 seconds so the title can display how many unread messages they have.

这就是我所拥有的试过:

Here is what I have tried:

setInterval(function() {
        document.title = "<?php echo $inboxcc; ?>";
    }, 3000);

$(function() {
setInterval(function() {
     $(this).attr("title", "<?php echo $inboxcc; ?>");
    }, 3000);
});

但它们都不起作用。

推荐答案

这种方法不起作用。你的PHP语句将在服务器端执行一次,所以无论你在JavaScript中做什么,标题都不会改变多次。

This approach won't work. Your PHP statement will execute once on the server-side, so no matter what you do in JavaScript, the title won't change more than once.

你需要一个AJAX基于成功的方法,将成功设置 document.title 属性:

You need an AJAX-based approach, which will set the document.title property on success:

$.ajax({
  url: 'new_page_title.php',
  data: {name: 'username', password: 'userpass'},
  success: function(data) { document.title = data;},
  dataType: 'text'
});

现在:你可以将这段代码塞进 setInterval 打电话,我建议每3秒检查一次可能会对你的服务器造成一点困难,而且没必要。每隔15-60秒就会更温和。

Now: while you certainly can tuck this code into a setInterval call, I would suggest that checking it every 3 seconds might get to be a bit hard on your server, and not necessary. Every 15 - 60 seconds would be gentler.

setInterval(function() {
    $.ajax({
        ...
    });
}, 30000); // milliseconds

这篇关于自动刷新文档标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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