【问题标题】:Long polling using Xampp使用 Xampp 进行长轮询
【发布时间】:2018-11-16 01:36:30
【问题描述】:

我有一个网页,它建立与 linux 主机的 ssh 连接并执行脚本。我想对在 linux 主机上运行的结果进行长轮询。

我使用 Xampp 作为 localhost 服务器。

这是我的 JS:

$(document).ready(function(){   

$(sub).click(function(){ 

    alert("connecting to host")
            $.ajax({
                type:'GET',
                url:'/cgi-bin/rfc.py', 
                async: false ,
                dataType: 'html',
                cache: false,
                data: $('form').serialize(),
                success:function (data) {
                     $('#output').html(data);


               }


        });

        }) ;
});

在这种情况下,我只有在脚本完成执行后才能得到结果。 我想在网页中每 15 秒查看一次输出(每 15 秒轮询一次)。任何建议都会对我很有帮助。 提前谢谢你。

【问题讨论】:

  • 关于 ajax 调用的 setInterval?还是简单的浏览器刷新?取决于页面的其他功能。但基本上你可以每 15 秒使用一次 ajax 函数,而不是 onclick。
  • 你的意思是在“url:'/cgi-bin/rfc.py'”上说setInterval??

标签: javascript jquery python-3.x xampp long-polling


【解决方案1】:

基本间隔类似于:

// Save the interval so we can stop it if needed by clearing the interval
let interval = null;
// Interval delay constant. 15s equals 15000 ms.
const DELAY = 15000;
// Render the output to the form.
const render = function( data ) {
    $( '#output' ).html( data );
};
// Trigger the ajax call every DELAY seconds.
const start_interval = function() {
    interval = setInterval( function() {
        const data = $('form').serialize();
        $.ajax({
            type:'GET',
            url:'/cgi-bin/rfc.py',
            dataType: 'html',
            cache: false,
            data: data,
            success: render
        });
    }, DELAY );
};
// Start using the interval once the webpage is loaded.
$(document).ready( start_interval );

【讨论】:

  • 非常感谢您。 !这会在每次延迟后运行我放置在“url”中的脚本。我需要脚本只运行一次。然后在每次延迟后获取该脚本的结果。你能帮我解决这个问题吗?
  • 我不明白你的意思。如果只调用一次ajax,脚本的结果不会改变。
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多