![](/img/trans.png)
[英]How to pass params of a 'delegated' function to another function from child to parents?
[英]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
}
};
您有几个简单的问题,必须导出将清除超时的函数
$.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.