jedihamster
jedihamster

Reputation: 63

Javascript to functions triggred on conditional argument

I am working with the following script and would ideally like to get two functions triggered on if (callOnce), I have tried the following (Example 1. - sync function) this works fine but where I add two functions, (example 2. - Sync and Fade functions). They both fail to produce a result. Any help appreciated...

Example 1.

function aperture() {
      "use strict";
      var media = document.getElementsByTagName("video")[0];
      if ((media.duration - media.currentTime) < 475)
          if (callOnce) {
              sync();
              callOnce = false;
          }
  }

Example 2.

function aperture() {
    "use strict";
    var media = document.getElementsByTagName("video")[0];
    if ((media.duration - media.currentTime) < 475)
        if (callOnce) {
            sync();
            fade();
            callOnce = false;
        }
}

Upvotes: 0

Views: 49

Answers (1)

Ismail RBOUH
Ismail RBOUH

Reputation: 10450

Please try this:

var aperture = (function() {
    var called = false;

    return function() {
        if(called) return;

        var media = document.getElementsByTagName("video")[0];
        if ((media.duration - media.currentTime) < 475) {
            sync();
            fade();
        }

        called = true;
    }

})();

Upvotes: 1

Related Questions