Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在循环中设置Timeout以检查边界的更改_Javascript_Google Maps_While Loop_Settimeout - Fatal编程技术网

Javascript 在循环中设置Timeout以检查边界的更改

Javascript 在循环中设置Timeout以检查边界的更改,javascript,google-maps,while-loop,settimeout,Javascript,Google Maps,While Loop,Settimeout,这是我的代码: var b; while(!b){ setTimeout(function(){ alert('sss') b=1; }, 500); } 它不会向“sss”发出警报 我能做什么 更新: 我想获得google maps v3的边界: function get_bounds(){ var bounds_; while(!bounds_){ setTime

这是我的代码:

var b;
while(!b){
    setTimeout(function(){
        alert('sss')
        b=1;
    }, 500);
}
它不会向“sss”发出警报

我能做什么

更新:

我想获得google maps v3的边界:

function get_bounds(){
            var bounds_;
            while(!bounds_){
                setTimeout(function(){
                    bounds_=map.getBounds();
                    if(bounds_){
                        var leftBottom=[bounds_.getSouthWest().lat(),bounds_.getSouthWest().lng()]
                        var rightTop=[bounds_.getNorthEast().lat(),bounds_.getNorthEast().lng()]
                        return [leftBottom,rightTop];
                        }
                }, 500);
            }
        }
更新2:

嗨,patrick dw,我不知道为什么,但您的代码不起作用:

var b;
function waitForB() {
    setTimeout(function(){
        if(!b)
            waitForB();
        else
            alert('sss');
    }, 500);
}
waitForB()
更新3:

现在没事了:

var b;
function waitForB() {
    setTimeout(function(){
        if(!b){
            waitForB();
            b='ss';
        }
        else{
            alert('sss')
        }
    }, 500);
}
waitForB()

这段代码将通过安排超时来消耗CPU时间和内存。想想看:你的循环条件是“b”变得真实。这将如何发生?仅当计时器事件触发时。那会发生吗?不,因为你要吃掉整台机器,还要安排数不清的超时

这种情况会让你坐的房间升温,这是一个很好的信号


我不知道你想得到什么效果。为什么不从调用
setTimeout()
开始,看看是怎么回事。也许你可以详细描述一下你想做什么。

web浏览器中的JavaScript是在一个线程中运行的。调用
setTimeout()
时,不会产生新线程。这意味着,
setTimeout()
将在所有主代码执行完毕后才会执行

因此,您将得到一个无限循环,因为循环条件取决于
setTimeout()
回调的执行

下面是一篇关于JavaScript计时器工作原理的有趣文章:

  • 约翰·雷西格

更新:

除了更新的问题之外,您可能还想听听
bounds\u changed
事件。我不确定您打算如何使用
get_bounds()
函数,但您可能希望重构逻辑以使用事件侦听器:

google.maps.event.addListener(map,'bounds_changed', function () {
   // The code here is triggered when the bounds change
});

也许您需要使用
setInterval
而不是
setTimeout
。 更改
b
时,将显示警报

var b = false;
(function () {
    var intervalId;
    function wait() {
        if (b) {
            clearInterval(intervalId);
            alert('sss');
        }
    }
    intervalId = setInterval(wait, 500);
})();
它更直观,不会太多地处理全局变量


提示:如果不确定在何处安全省略,请在每条语句后加分号。

现在可以使用
idle
而不是
bounds\u changed
事件侦听器正确解决此问题:

google.maps.event.addListener(map, 'idle', function() { 
  updateStuff();
});
当平移或缩放后贴图变为空闲时,将触发此事件。


它也会在地图首次呈现后触发,因此这可能是地图上保持最新状态所需的唯一事件侦听器。

您在警报后忘记了分号(…)是的,这是真的,但分号在Javascript中并不总是必须的。@Radomir:感谢Javascript,代码是
实际上不会有什么不同。在没有
的情况下,JS将采用换行符作为行分隔符
或不在
(…)
块中。如果您只是尝试每隔
500ms运行一次代码,直到获得
边界,请参见下面的我的答案。嗨,丹尼尔,看看更新的,我想获得google maps v3的边界,但有时我无法获得,所以我必须稍后获得它,那么我该怎么办呢?@zjm1126:你为什么不听听
bounds\u changed
事件呢<代码>google.maps.event.addListener(映射,'bounds_changed',函数(){})