如何执行一个函数,在其中是另一个函数并传递params

时间:2016-06-21 07:27:43

标签: javascript

我有这样的功能:

private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
   NavigationWindow window = new NavigationWindow();
   window.Source = new Uri("Page1.xaml", UriKind.Relative);
   window.Show();
}

我的问题是如何调用$.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 param传递给它。

我已经尝试了uuid,但它会抛出错误。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

您必须封装到函数中:

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

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

答案 2 :(得分:0)

你有几个简单的问题,你必须导出clearTimeout

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