0

I have a problem using setTimeout. I have been using it lots of times and it always works the way I expect it to. Even when I put the code in a separate function it works but just in this case it has a weird behaviour.

Here is a the very code:

RoutePatternPoint.prototype.showOnMap = function(map)
{
    var that = this;
    google.maps.event.addListener(this.marker, "click", function(event)
    {
        window.setTimeout(function(){

            if(RoutePatternPoint.markerDoubleClickFix === false) { doSomething(); }

             RoutePatternPoint.markerDoubleClickFix = false;

          },350);
    }); 

    google.maps.event.addListener(this.marker, "dblclick", function(event)
    {
         RoutePatternPoint.markerDoubleClickFix = true;

    });
}

The problem is that google maps api v3 has a bug when single and double click events are implemented - both of them gets executed.

So the solution is to slow down the single click event and see if a double click event is executed. If the double click event is executed then we interrupt the single click event.

Unfortunately my code is not working the way I want it to so I decided to write some alert function and see what happens. Guess what? The setTimeout function inside the single click event is executed twice.

What am I doing wrong?

6
  • It's possible that the timeout elapses before the double click is fired. Try to augment the timeout and check whether it's still happening Commented May 9, 2013 at 18:22
  • 1
    Could it be that a double click action actually fires off two single click events (and a double click event)? Commented May 9, 2013 at 18:53
  • Also RoutePatternPoint.markerDoubleClickFix === false is not going to be true the first time around, unless you explicitly set that to false somewhere else. Probably better to use !RoutePatternPoint.markerDoubleClickFix or ==. Commented May 9, 2013 at 18:58
  • 1
    @DaggNabbit The single click event only fires once for me. It probably depends on platforms. Another possibility is showOnMap() was called twice and the listener was added twice. Commented May 9, 2013 at 19:13
  • 1
    @LeiZhao, hmm, that could be it. No way to tell, really. For the record, the FLTK library is the only thing that really got double (and triple, quadruple, whatever) click events right, IMO. Each click is just a click, but it carries with it the number of previous clicks in some short amount of time. So if you're looking for a double click, you can just check if that number is greater than 0. /OT Commented May 9, 2013 at 19:23

1 Answer 1

1

You should debounce (https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf) the user clicks:

RoutePatternPoint.prototype.showOnMap = function (map) {
    var that = this;
    google.maps.event.addListener(this.marker, "click", function (event) {
        if (that.clickHandler) {
            window.clearTimeout(that.clickHandler);
        }
        that.clickHandler = window.setTimeout(function () {
            doSomething();
        }, 350);
    }); 
    google.maps.event.addListener(this.marker, "dblclick", function (event) {
        if (that.clickHandler) {
            window.clearTimeout(that.clickHandler);
            that.clickHandler = null;
        }
    });
}

I don't think Google Maps API v3 firing both single click and double click events is a bug. This is useful in many cases.

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

1 Comment

This looks like simple debounce / throttle, you can read more about it here: codeburst.io/…

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.