Alireza Valizade Alireza Valizade - 1 month ago 15
Javascript Question

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

I have a function like this :

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


My problem is how to call the
Interval
function and pass
uuid
param to that.

I have tried
$.SetInLocalStorageVideoTime("blahblah").Interval();
but it throws an error.

Answer
var Interval = setInterval(...)

This immediately calls the setInterval function and assigns its return value to Interval; same for clearInterval. You don't want to call the function, you want to create a function which when called calls the function. Two ways to do that:

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

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

Putting it all together, you want this:

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