OpenWrt Forum Archive

Topic: Openwrt HR.poll, update web field every X second

The content of this topic has been archived on 2 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Trying to understand how things work in OpenWrt system.

I've created new field in Luci index page:

<fieldset class="cbi-section">
    <legend><%:Test Legend%></legend>">
    <table width="100%" cellspacing="10">
    <tr>
        <td width="33%"><%:Random%></td>
        <td id="rand"<%=luci.sys.exec('head -c /dev/random')%></td>
    </tr>
   </table>
</fieldset>

Then I try to refresh this field every 4 second:

XHR.poll(4, '<%=REQUEST_URI%>' null,
    function(x, data)
    {
    var test = <%=luci.sys.exec('head -c /dev/random')%>;           
    var random = document.getElementById('rand');
    random.innerHTML = test;
    }                                                       
);

Result:

    1. The field in an index page reloads with new random value.
    2. After 4 seconds updates with another one.
    3. And that's all, doesn't update every 4 seconds as expected.

Looking for a help with JavaScript, how make the process permanent.

Thanks in advance.

You are trying to write Lua value in JS variable, it may cause problem:
     "var test = <%=luci.sys.exec('head -c /dev/random')%>; "

Try using this:
    var test = <%=luci.http.write_json(luci.sys.exec('head -c /dev/random'))%>;

The discussion might have continued from here.