显示器通过AJAX,如何文件更改? [英] Monitor file change through AJAX, how?

查看:116
本文介绍了显示器通过AJAX,如何文件更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找通过AJAX的方式(而不是通过JS框架!)实时监视更改的文件。如果在这里取得了该文件的变化,我需要它给一个警告信息。我是一个总的AJAX小白,所以请温柔。 ; - )

I'm looking for a way through AJAX (not via a JS framework!) to real time monitor a file for changes. If changes where made to that file, I need it to give an alert message. I'm a total AJAX noob, so please be gentle. ;-)

编辑:让我解释的目的多一点的细节。我用我用PHP编写的webhop聊天脚本,我要的是从管理模块监控聊天请求。该聊天记录存储在文本文件,如果有人开始创建一个新的文件聊天会话。如果是这样的情况下,管理模块中我希望看到的实时性。

let me explain the purpose a bit more in detail. I'm using a chat script I've written in PHP for a webhop, and what I want is from an admin module monitor the chat requests. The chats are stored in text files, and if someone starts a chat session a new file is created. If that's the case, in the admin module I want to see that in real time.

有道理?

推荐答案

要监视与AJAX改变,你可以做一些这样的文件。

To monitor a file for changes with AJAX you could do something like this.

var previous = "";

setInterval(function() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            if (ajax.responseText != previous) {
                alert("file changed!");
                previous = ajax.responseText;
            }
        }
    };
    ajax.open("POST", "foo.txt", true); //Use POST to avoid caching
    ajax.send();
}, 1000);

我只是测试它,和它的作品pretty的好,但我仍然认为AJAX是不是要走的路在这里。文件内容比较将是很大的文件速度慢。此外,您mentionned没有框架,但你应该使用一个AJAX的,只是为了处理跨浏览器的不一致。

I just tested it, and it works pretty well, but I still maintain that AJAX is not the way to go here. Comparing file contents will be slow for big files. Also, you mentionned no framework, but you should use one for AJAX, just to handle the cross-browser inconsistencies.

这篇关于显示器通过AJAX,如何文件更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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