views:

115

answers:

3

This is my code:

var b;
while(!b){
    setTimeout(function(){
        alert('sss')
        b=1;
    }, 500);
}

and it will not alert 'sss'

What can i do?

Updated:

I want to get bounds on 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);
            }
        }

updated2:

hi patrick dw, i don't know why , but your code doesn't work:

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

updated3:

it is ok now :

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

That code is going to burn CPU time and memory by scheduling timeouts to happen. Think about it: you're loop condition is "b" becoming truthy. How is that going to happen? Only when a timer event fires. Will that happen? No, because you're eating the whole machine scheduling zillions more timeouts.

This sort of situation has as a tell-tale sign the effect of warming up the room you're sitting in.

I don't know what effect you're trying to get. Why not start by just the setTimeout() call and see how that goes. Maybe you could describe more about what it is you're trying to do.

Pointy
+5  A: 

JavaScript in web browsers is run in a single thread. When you call setTimeout() it won't spawn a new thread.

You will end up with an infinite loop, because the setTimeout() callback will never be called.

Here's an interesting article on how JavaScript timers work:


UPDATE:

Further to the updated question, you may want to listen to the bounds_changed event instead. I am not sure how you are planning to use your get_bounds() function, but you may want to refactor your logic to use an event listener instead:

google.maps.event.addListener(map,'bounds_changed', function () {
   // The code here is triggered when the bounds change
});
Daniel Vassallo
hi Daniel, look the updated, i want get the bounds of google maps v3, but some times i can't get it , so i have to get it sometimes later, so what can i do about it ..
zjm1126
@zjm1126: Why don't you listen to the `bounds_changed` event? `google.maps.event.addListener(map,'bounds_changed', function () { });`
Daniel Vassallo
@Daniel it is a good idea .
zjm1126
A: 

Maybe you will want to use setInterval instead of setTimeout. When b is changed, alert shows up.

var b = false;
(function () {
    var intervalId;
    function wait() {
        if (b) {
            clearInterval(intervalId);
            alert('sss');
        }
    }
    intervalId = setInterval(wait, 500);
})();

It is more intuitive and it doesn't mess with global variables too much.

HINT: Put semicolon after every statement if you are not sure where to omit safely.

pepkin88