javascript - 为什么 setTimeout 如此善变且半功能性?

标签 javascript jquery settimeout

function load_result()
{
    $.getJSON( "http://somesite.com/loadresult.php", function(data) {
        if( data == undefined || data == null )
        {
            console.log( "no data" );
            clearTimeout( lr_timeout );
            lr_timeout = setTimeout( "load_result()", 2000 );
            return;
        }
        insert_result_price( data );
        clearTimeout( lr_timeout );
        lr_timeout = setTimeout( "load_result()", 50 * ( Math.random() * 10 ) );

    } );
}

lr_timeout是全局定义的,load_result函数最初是在document.ready函数中启动的。问题是该函数并不总是运行。我将在 Firebug 中观看它,并且我有另一个在始终有效的 setInterval 上设置的函数。

想法?

最佳答案

您的另一个 setTimeout 函数(始终有效的函数)是否也位于 $.getJSON 中?如果是这样,它是否在相同的 URL 上调用它(在您的示例中为 http://somesite.com/loadresult.php ,但我确信它在现实生活中有所不同)?

http://somesite.com/loadresult.php 可能是一个不稳定的资源,并且结果不一致是由该资源引起的,而不是 setTimeout。当然,如果他们都使用相同的资源,那么这就不是问题。

关于javascript - 为什么 setTimeout 如此善变且半功能性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1992280/

相关文章:

javascript - 为什么 setTimeout(.., 0) 不立即执行?

javascript - CORS 奇怪的行为

javascript - 使用选择器作为变量循环 .append()

jquery - 是否可以停止使用 jQuery 调整浏览器窗口的大小?

php - 替换图像(通过单击图像)

javascript - 如何通过套接字事件停止setTimeOut?

javascript - for循环中的setTimeout

javascript - Php 创建文件并更改位置不起作用

php - 设计投票系统

javascript - 根据类名更改 vue.js 中的背景颜色