简体   繁体   English

如何从setInterval()调用js函数

[英]How to call js Function from setInterval()

AIM: I would like to set the below function to call every 5 seconds. 目的:我想将下面的函数设置为每5秒调用一次。 Using qtip. 使用qtip。

The variable ALARM_POPUP changes every minute via a shellscript which replaces the contents on the variable. 变量ALARM_POPUP通过shell脚本每分钟更改一次,该脚本替换了变量上的内容。

CODE: 码:

    function popupTXT()

         {

              var ALARM_POPUP="Alarm Warning warning ";

              $('#telecom_1').qtip({content:ALARM_POPUP,style: {name: 'dark', tip: 'topLeft'} });

        };

I am using the below to call on a timer. 我正在使用下面的计时器。

    setInterval("popupTXT()",5000);

OUTCOME: This will only work when I refresh the browser. 结果:这仅在刷新浏览器时有效。 Any suggestions would be appreciated 任何建议,将不胜感激

Try this: 尝试这个:

setInterval(popupTXT,5000);

Just a sample 只是一个样本

Read more about .setInterval() 进一步了解.setInterval()

The variable ALARM_POPUP changes every minute via a shellscript which replaces the contents on the variable. 变量ALARM_POPUP通过ALARM_POPUP每分钟更改一次,该ALARM_POPUP替换了变量上的内容。

That means that in order to see that change on the page, you have to call the server to get an updated value. 这意味着要在页面上看到该更改,您必须调用服务器以获取更新的值。 You're not doing that. 你没那么做

You could do that via ajax . 您可以通过ajax做到这一点。 Create a server-side page that outputs the new value for ALARM_POPUP as raw text (using Content-Type: text/plain ) or as JSON (using Content-Type: application/json ), and trigger an ajax call to get the contents of that page, then update the qtip with it. 创建一个服务器端页面,该页面以原始文本(使用Content-Type: text/plain )或JSON(使用Content-Type: application/json )输出ALARM_POPUP的新值,并触发ajax调用以获取以下内容:该页面,然后使用它更新qtip You wouldn't want setInterval for that because with the indeterminate length of time the ajax call would take, things would very quickly become chaotic. 您不希望使用setInterval ,因为随着ajax调用花费的时间不确定,事情很快就会变得混乱。 Instead, just initiate a setTimeout upon completion of the previous cycle. 相反,只需在上一个周期完成后启动setTimeout

Assuming you create an updatealarm.xyz page (PHP, JSP, ASP.Net, whatever) that outputs the current ALARM_POPUP value as plain text, that would look something like this: 假设你创建一个updatealarm.xyz页面(PHP,JSP,ASP.Net,等等),输出电流ALARM_POPUP值以纯文本格式,这将是这个样子:

(function()
{
    var ALARM_POPUP="Alarm Warning warning ";

    function updateQtip(popupText)
    {
        $('#telecom_1').qtip({content:popupText,style: {name: 'dark', tip: 'topLeft'} });
        setTimeout(nextUpdate, 5000);
    }

    function nextUpdate()
    {
        $.ajax({
            url: "updatealarm.xyz",
            success: function(data)
            {
                ALARM_POPUP = data; // Although you don't actually need to update it
                updateQtip(data);
            },
            error: function()
            {
                // Do error handling
            }
        });
    }

    updateQtip();

})();

About your original setInterval call: It's best not to pass strings into setInterval or setTimeout ; 关于原始setInterval调用:最好不要将字符串传递到setIntervalsetTimeout that's basically doing an eval , and it's neither necessary nor a good idea. 这基本上是在进行eval ,这既不是必要的,也不是一个好主意。 Instead, pass in a function reference (eg, the function's name, without () calling it), as above. 相反,如上所述,传递函数引用(例如,函数名称,而不用()调用它)。


Re your comment below: 在下面重新发表您的评论:

I am having problems with this and I was wondering if you provide an example of what the php file would look like 我对此有疑问,我想知道您是否提供一个有关php文件的示例

I've only done a little PHP, but I believe it would look like this: 我只做了一点PHP,但是我相信它看起来像这样:

<?php
    header('Content-Type: text/plain');
    echo 'This is the message that will end up in \'data\' in the ajax success handler.';
?>

Or if you prefer to use a variable to make it easier for your sed script: 或者,如果您更喜欢使用变量来sed脚本,则:

<?php
    header('Content-Type: text/plain');
    $alarm_popup = 'This is the message that will end up in \'data\' in the ajax success handler.';
    echo $alarm_popup;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM