Web2py中的Javascript轮询

时间:2012-11-30 22:12:32

标签: javascript jquery ajax web2py

我将此代码放入主视图文件中:

    <html>
            <head>
           <script src="/app/static/js/jquery.js" type="text/javascript">
            <script type="text/javascript">
                jQuery(document).ready(function() {
                    window.setTimeout(function() {
                        var url = '{{=URL('monitor.load')}}';
                        jQuery('#monitor').load(url);
                    }, 10000);
                });
            </script>
            </head>

            <body>
                <div id="monitor"></div>
            </body>
        </html>

为了在#monitor div标签中显示来自monitor.load文件的数据:

{{=data}}

但是,它在10秒后只放置一次数据,然后它不像我想的那样每10秒更新一次数据...
如果在控制器中更改了数据,或者在monitor.load文件中更改了数据 什么都没发生......

1 个答案:

答案 0 :(得分:4)

如果您希望代码多次出现,则应使用setInterval()

示例:

setInterval("alert('hello!');", 500);

上面的代码将从加载页面到关闭页面每500毫秒执行一次代码alert('hello!');