繁体   English   中英

如何执行内部另一个函数的函数并传递参数

[英]how to execute a function that inside it is another function and pass params

我有这样的功能:

$.SetInLocalStorageVideoTime = function (uuid) {

    alert(uuid);
    var Interval = setInterval(function () {

        localStorage.setItem('poption-ctime-'+ uuid , jwplayer("target").getPosition());

    },10000);

    var ClearInterVal = clearInterval(Interval);

    return {

        Interval : Interval,
        ClearInterVal : ClearInterVal
    }
};

我的问题是如何调用Interval函数并将uuid参数传递给该函数。

我已经尝试过$.SetInLocalStorageVideoTime("blahblah").Interval(); 但是会引发错误。

var Interval = setInterval(...)

这将立即调用 setInterval函数,并将其返回值分配给Interval ; 对于clearInterval相同。 您不想调用该函数,而是要创建一个在调用时调用该函数的函数。 有两种方法:

var Interval = function () {
    setInterval(...);
}

var Interval = setInterval.bind(null, ...);

放在一起,您需要这样:

$.SetInLocalStorageVideoTime = function (uuid) {
    var interval = null;

    var set = function () {
        interval = setInterval(function () {
            localStorage.setItem('poption-ctime-'+ uuid , jwplayer("target").getPosition());
        }, 10000);
    };

    var clear = function () {
        clearInterval(interval);
    };

    return {
        Interval : set,
        ClearInterVal : clear
    }
};

看看这个笨蛋: https ://plnkr.co/edit/7H61Vv6m8M552CNeIpSA ? p = preview

您必须封装成函数:

var stop;
var interval =  function () { 
    stop = setInterval(function () {
        console.log(uuid);
    },100);
}

var ClearInterVal = function () { clearInterval(stop) };

您有几个简单的问题,必须导出将清除超时的函数

$.SetInLocalStorageVideoTime = function(uuid) {
  // auto start interval, you could
  // add starter function or something
  var Interval = setInterval(function() {
    localStorage.setItem('poption-ctime-' + uuid, jwplayer("target").getPosition());
  }, 10000);

  // clear function
  // exported
  var ClearInterVal = function() {
    if (Interval)
      clearInterval(Interval);
  }
  return {
    // Interval is not required here
    ClearInterVal: ClearInterVal
  }
};
$.SetInLocalStorageVideoTime();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM