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

Javascript 如何执行内部是另一个函数的函数并传递参数,javascript,Javascript,我有这样一个函数: $.SetInLocalStorageVideoTime = function (uuid) { alert(uuid); var Interval = setInterval(function () { localStorage.setItem('poption-ctime-'+ uuid , jwplayer("target").getPosition()); },10000); var ClearInterVal

我有这样一个函数:

$.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()但它抛出了一个错误。

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

$.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();

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

$.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();
这会立即调用
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
    }
};
这会立即调用
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
    }
};
看看这个笨蛋:

您必须封装到函数中:

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

var ClearInterVal = function () { clearInterval(stop) };
看看这个笨蛋:

您必须封装到函数中:

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

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

它抛出了什么错误?
uncaughttypeerror:$.SetInLocalStorageVideoTime不是函数(…)
uncaughttypeerror:$.SetInLocalStorageVideoTime(…).Interval不是函数
$.SetInLocalStorageVideoTime(“blahblah”)将间隔作为值而不是函数返回,因此不能调用
Interval
是间隔的
id
,是一个数字,不是函数!它抛出了什么错误?
uncaughttypeerror:$.SetInLocalStorageVideoTime不是函数(…)
uncaughttypeerror:$.SetInLocalStorageVideoTime(…).Interval不是函数
$.SetInLocalStorageVideoTime(“blahblah”)将间隔作为值而不是函数返回,因此不能调用
Interval
是间隔的
id
,是一个数字,不是函数!