clearInterval()无法正常工作 [英] clearInterval() not working correctly

查看:156
本文介绍了clearInterval()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直试图重复一个jQuery AJAX请求每两秒钟。请注意:我已经看了SO上,而不是他们的其他问题似乎适用于我的情况,我也看了看文档

I've recently been trying to repeat a jQuery AJAX request every two seconds. PLEASE NOTE: I've looked at the other questions on SO and not of them seem to apply to my situation, i've also looked at the documentation.

摘录:

else if (text == "load") {
        $(".response").text("working!");

        var traffic = function load() {
            $.ajax({url:"load.php",success:function(result){
                var obj = jQuery.parseJSON(result);
                var ids = obj.map(function(el) {
                    return "<tr><td>" + el.id + "</td><td>" + el.ip + "</td><td>" + el.proxyip + "</td><td>" + el.ping + "</td><td>" + el.time + "</td></tr>";
                });

                $(".response").html("<table><tr><td><strong>ID</strong></td><td><strong>IPs</strong></td><td><strong>Proxy IP</strong></td><td><strong>Ping</strong></td><td><strong>Time</strong></td></tr>" + ids + "</table>");
            }});
        };

        var trafficInterval = setInterval(traffic, 2000);

要CANCLE超时,我检查,看看如果文本不等于负荷,如果它不是等于加载,我CANCLE超时

To cancle the timeout, i check to see if the text is not equal to "load", if it isnt equal to load, i cancle the timeout

else {
        $(".response").text("'" + badCommand + "'" +" is not a valid command, type 'help' for a list of commands.");
        clearInterval(trafficInterval);
    }

然而,当我改变文本框的输入,该表仍然会每两秒钟加载,超时还未cancled,我似乎无法明白为什么。

HOWEVER, when i change the input of the textfield, the table will still load every two seconds, the timeout hasn't been cancled and i can't seem to see why.

下面是整个功能的人不知道

Here's the whole function for people wondering

$("textarea").keyup(function(){
var text = $(this).val();
$(".log").text(text);

// Commands
if (text != "") {
    var badCommand = $("textarea").val();

    if (text == "help") {
        $(".response").html("Commands:<br/><span class='indent'>traffic -url|all</span><span class='indent'>google #search term</span><span class='indent'>youtube #search term</span><span class='indent'>portfolio</span>")
    } else if (text == "traffic") {
        $(".response").html('Live traffic from:<br/><table><tr><td><strong>IP</strong></td><td><strong>Proxy IP</strong></td><td><strong>Ping</strong></td><td><strong>Time</strong></td></tr><?php try { $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_db", $mysql_user, $mysql_pass); $sql = "SELECT * FROM traffic ORDER BY id DESC LIMIT 50"; foreach ($db->query($sql) as $row) { echo "<tr>"; echo "<td class='details'>" . $row["ip"] . "</td>"; echo "<td class='details'>" . $row["proxyip"] . "</td>"; echo "<td class='details'>" . $row["ping"] . "</td>"; echo "<td class='details'>" . $row["time"] . "</td>"; echo "</tr>"; } $db = null; } catch(PDOException $e) { $e->getMessage(); } ?></tr></table>');
    } else if (text == "load") {
        $(".response").text("working!");

        var traffic = function load() {
            $.ajax({url:"load.php",success:function(result){
                var obj = jQuery.parseJSON(result);
                var ids = obj.map(function(el) {
                    return "<tr><td>" + el.id + "</td><td>" + el.ip + "</td><td>" + el.proxyip + "</td><td>" + el.ping + "</td><td>" + el.time + "</td></tr>";
                });

                $(".response").html("<table><tr><td><strong>ID</strong></td><td><strong>IPs</strong></td><td><strong>Proxy IP</strong></td><td><strong>Ping</strong></td><td><strong>Time</strong></td></tr>" + ids + "</table>");
            }});
        };

        var trafficInterval = setInterval(traffic, 2000);

    } else {
        $(".response").text("'" + badCommand + "'" +" is not a valid command, type 'help' for a list of commands.");
        clearInterval(trafficInterval);
    }
}

if (text == "") {
    var noCommand = $("textarea").val();
    $(".response").text(" ");
}

// End Commands

});

供参考:

把调用clearInterval();后在code的工作停止了,
把调用clearInterval();之前,code什么都不做。

Putting the clearInterval(); AFTER the code stops it from working,
Putting the clearInterval(); BEFORE the code does nothing.

调用clearInterval(trafficInterval);必须正常工作,因为如果我把它放在第一个函数的末尾,当我键入加载

The clearInterval(trafficInterval); MUST be working because if i place it at the end of the first function, nothing happens when i type "load"

这不是定时器要么,我试了一下,只要鼠标移动和同样的事情发生......而不是自动更新。

It's not the timer either, i tried it instead to automatically update whenever the mouse is moved and the exact same thing happens...

推荐答案

这是一个范围的问题。间隔需要是事件处理程序呼叫的范围之外:

This is a scoping issue. The interval needs to be outside the scope of the event handler call:

var trafficInterval = null;
$("textarea").keyup(function(){
var text = $(this).val();
$(".log").text(text);

// Commands
if (text != "") {
    var badCommand = $("textarea").val();

    if (text == "help") {
        $(".response").html("Commands:<br/><span class='indent'>traffic -url|all</span><span class='indent'>google #search term</span><span class='indent'>youtube #search term</span><span class='indent'>portfolio</span>")
    } else if (text == "traffic") {
        $(".response").html('');
    } else if (text == "load") {
        $(".response").text("working!");

        var traffic = function load() {
            $.ajax({url:"load.php",success:function(result){
                var obj = jQuery.parseJSON(result);
                var ids = obj.map(function(el) {
                    return "<tr><td>" + el.id + "</td><td>" + el.ip + "</td><td>" + el.proxyip + "</td><td>" + el.ping + "</td><td>" + el.time + "</td></tr>";
                });

                $(".response").html("<table><tr><td><strong>ID</strong></td><td><strong>IPs</strong></td><td><strong>Proxy IP</strong></td><td><strong>Ping</strong></td><td><strong>Time</strong></td></tr>" + ids + "</table>");
            }});
        };

        trafficInterval = setInterval(traffic, 2000);

    } else {
        $(".response").text("'" + badCommand + "'" +" is not a valid command, type 'help' for a list of commands.");
        clearInterval(trafficInterval);
    }
}

if (text == "") {
    var noCommand = $("textarea").val();
    $(".response").text(" ");
}

// End Commands
});

请参阅小提琴@ http://jsfiddle.net/sLooj4on/ (可以看到查询无法在开发工具,停在文本从输入删除)

See fiddle @ http://jsfiddle.net/sLooj4on/ (can see polling fail in dev tools, stops when text is removed from input)

这篇关于clearInterval()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆