Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions content_scripts running multiple times #11

Closed
aculligan opened this issue Jun 30, 2019 · 2 comments

Comments

Projects
None yet
1 participant
@aculligan
Copy link
Owner

commented Jun 30, 2019

Functions in content_scripts run multiple times, causing Google Analytics to fire several times and local variables get reset to undefined.

@aculligan aculligan added this to the Firefox implementation milestone Jun 30, 2019

@aculligan aculligan self-assigned this Jun 30, 2019

@aculligan

This comment has been minimized.

Copy link
Owner Author

commented Jun 30, 2019

Main function in content_scripts runs on three events (load, focus, click).

$(document).ready(function () {
  executeAll(2000);
});
$(window).focus(function () {
  executeAll(1300);
});
$('body').click(function () {
  executeAll(1000);
});

Need to set a global guard variable to stop function from running more than once. Might get performance improvement too?

@aculligan aculligan changed the title Google Analytics causes content_scripts issues Functions content_scripts running multiple times Jul 1, 2019

@aculligan

This comment has been minimized.

Copy link
Owner Author

commented Jul 1, 2019

This happens if another event fires the main function before the setTimeout of another event completes. For example, if someone focuses the window and also clicks within the 1.3 seconds it takes for the main function ran by the focus event to complete.

Fixed with a global variable

let busyScript = window.hasRun; // Will be undefined

const executeAll = function (time) {
  if (window.location.href.indexOf('agent/filters') > 0 && busyScript !== true) {
    busyScript = true; // sets to true to stop from running again before it finishes
    setTimeout(function () {
      /*
      * rest of the code goes here
      */
      busyScript = false; // sets back to false
    }, time);
  }
}

$(document).ready(function () {
  executeAll(2000);
});
$(window).focus(function () {
  executeAll(1300);
});
$('body').click(function () {
  executeAll(1300);
});

Tested in Chrome. Will deploy with v1.2.0.

@aculligan aculligan closed this Jul 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.