Javascript to functions triggred on conditional argument

时间:2016-07-11 21:51:35

标签: javascript

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;
        }
}

1 个答案:

答案 0 :(得分:1)

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;
    }

})();