2

I have a file that gets new lines appended to it frequently. I want to create a web application running in the browser using javascript that will be able to detect changes on that local file and print the new content that is added on the file.

My solution includes using javascript polling with setInterval and the new HTML5 File API. I load the file using an input file field and then poll the size of the file. Each time the size changes (in my case increases) this indicates to me that new content has been added to the file. I then slice the file using a Blob and only take the content that was appended using the old file size as start and the new file size as end.

Generally it works good with an average interval (I'm using 500ms) but I'm curious if there's a better solution cause this one sounds a bit like a hack to me.

4

1 に答える 1

-1

tail -f のような次のファイルにjs-logtailを使用できます。

<head>
    <title>habitat parser log viewer</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="logtail.js"></script>
    <link href="logtail.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div id="header">
        js-logtail.
        <a href="./">Reversed</a> or
        <a href="./?noreverse">chronological</a> view.
        <a id="pause" href='#'>Pause</a>.
    </div>
    <pre id="data">Loading...</pre>
</body>

サーバー側の部分さえ必要ありません。Web サーバー (Apache、lighthttpd など) は、内部で HTTP Range ヘッダーを使用してこれを処理します。

于 2015-08-30T08:58:23.170 に答える