gzog / analytics-issues

Automatically exported from code.google.com/p/analytics-issues
0 stars 0 forks source link

createFunctionWithTimeout Example Bug #872

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Name of related component: Documentation

URL of the documentation page :

https://developers.google.com/analytics/devguides/collection/analyticsjs/sending
-hits

Issue summary:

The createFunctionWithTimeout will not work as written. The callback is being 
passed to setTimeout and will always execute.

function createFunctionWithTimeout(callback, opt_timeout) {
  var called = false;
  setTimeout(callback, opt_timeout || 1000);
  return function() {
    if (!called) {
      called = true;
      callback();
    }
  }
}

Might be better to use the setTimeout return value so it can be cleared. Maybe 
something like the following:

function createFunctionWithTimeout(callback, opt_timeout) {
  var id = null;
  var f = function() {
    if(id !== null) {
      clearTimeout(id);
      id = null;
      callback();
    }
  };
  id = setTimeout(f, opt_timeout || 1000);
  return f;
};

Original issue reported on code.google.com by ohn...@gmail.com on 5 Feb 2016 at 4:25