2

I am using google maps for a project. I need to be able to hide and display multiple maps. I cannot use a basic toggleDiv type function alone. This is because Google maps will ignore the intended size of the div when the div is set to display: none from CSS. (for whatever reason it is fine with being toggled by javascript.) I could use GSize(width, height) but it cannot handle percentages. Since I need the map to be 100%, 100%, this is not an option. I figured out a way around it which is to call the second map's function using onClick, rather then loading all the functions using body onLoad. But, then the zoom of the map is not saved and the map is just reloaded.

So, I need to check if a function has been called, and if so, do not recall it. I cannot figure out how to do this. Any help is appreciated.

Thank you.

2
  • Do you have control over the code of this function? Commented Jul 6, 2011 at 20:16
  • For a prettier solution - see teresko's answer. Tracking variables are kept INTERNAL to the function and don't clutter the outer scope. Commented Jul 6, 2011 at 20:33

4 Answers 4

7

Another "lazy" variant:

var doOnce = function () {
    // do something

    doOnce = function () {}
}

As for the original problem, if the function you want to run once is the click event handler, you'd better remove the click event listener.

Sign up to request clarification or add additional context in comments.

Comments

2

Define a variable as such. var beenFired = false;

in the function do this:

function myFunction()
{
    if(!beenFired)
    {
        //TODO FUNCTION
        beenFired = true;
    } 
}

That will check to see if hasn't been fired yet and if not fire it. You could even do it around the function call itself.

if(!beenFired)
    myFunction()

function myFunction()
{
    //TODO FUNCTION
    beenFired = true;
}

4 Comments

I literally just figured it out. I tried to answer my own question but it wouldn't let me. Thanks!
I believe you're allowed to answer your own question, but only after some time period has passed. You can always edit your original question to include additional discoveries.
Yes you able to so, just as @jfriend00 said you'll have to wait a certain amount of time.
Yeah, I was just going to edit it but saw so many people answered it didn't matter. Sometimes you just need to talk it out before you get that thud! moment. Appreciate all the responses.
1
var func = (function(){
    var first = true;
    return function( a, b, c){
        if ( !first ){
            return;
        }
        first = false;

        console.log( arguments );
    }
})();

func( 1, 2, 3); // in console : [ 1, 2, 3]
func( 1, 1, 1); // nothing happens;

1 Comment

It's a waste of computation (the condition is evaluated on every call), see my answer below.
1

If I understand:

var isCalled = false;

function f() {
  if (!isCalled) {
    isCalled = true;
    // ...
  }
}

5 Comments

Oh, first I thought, that it is necessary to keep only 1 working function at single moment. Thanks, I've fixed.
At single moment? You mean during an Ajax request?
I mean to prevent second calling of the function while the first one is running. It is often situation for some event handlers. But I've read question carefully and fixed my answer.
Just making sure you understand that there can't be simultaneous calls in synchronous environment (which JavaScript is).
Yea, I understand. JS is a sync. But it could be an ajax call in the func body, as you told in 3rd comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.