0

I have base class Timer and a couple of methods inside it. My idea is simple, I need a basic class to run the timer. A lot of various timers as the instances of base class. But I have no idea how should I pass method as a parameter inside the .startTimer and .run

function Timer(interval) {
        this.isTimerRuns = false;
        this.timerId = 0;
        this.interval = interval;
    }

Timer.prototype.run = function (foo) {
    if(this.isTimerRuns){
        foo();
        setTimeout(this.run(foo), this.interval);
    }
    else{
        this.stopTimer();
    }
};

Timer.prototype.startTimer = function (foo) {
    this.timerId = setTimeout(this.run(foo), this.interval);
    this.isTimerRuns = true;
};

Timer.prototype.stopTimer = function () {
    clearInterval(this.timerId);
    this.isTimerRuns = false;
    this.timerId = 0;

};

Timer.prototype.ajaxCall = function (url, method, successFunc, errorFunc) {
    $.ajax({
        url: url,
        type: method,
        success: function (data) {
            var respond = JSON.parse(data);
            successFunc(respond);
        },
        error: function () {
            if(errorFunc != null){
                errorFunc();
            }
        }
    });
};

When I try to run my crap like this:

    var t = new Timer(10000);
    t.startTimer(t.ajaxCall("/123", "POST", test2, null));

function test2(resp) {
    console.log(resp + '!');
}

it runs only once and stops. How can I fix it?

2 Answers 2

3

It runs once cause you execute the function instead of passing it as a parameter:

Do like this:

t.startTimer( function() { t.ajaxCall("/123", "POST", test2, null); });

Providing that the rest of your code does what you want, that should do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. good spot. As well as run method does not retain the timeout reference. So perhaps the whole class needs some rethinking.
0

function Timer(interval) {
        this.isTimerRuns = false;
        this.timerId = 0;
        this.interval = interval;
    }
Timer.prototype.startTimer = function (foo) {
	this.isTimerRuns = true;
    this.timerId = setInterval(foo, this.interval);
    
};

Timer.prototype.stopTimer = function () {
	clearInterval(this.timerId);
    this.isTimerRuns = false;
    this.timerId = 0;

};

Timer.prototype.ajaxCall = function (url, method, successFunc, errorFunc) {
    $.ajax({
        url: url,
        type: method,
        success: function (data) {
            var respond = JSON.parse(data);
            successFunc(respond);
        },
        error: function () {
            if(errorFunc != null){
                errorFunc();
            }
        }
    });
};

function test2(resp) {
    console.log(resp + '!');
}

  var t = new Timer(1000);
    t.startTimer(function(){t.ajaxCall("/123", "POST", test2, null)});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

In javascript setInterval function available to call infinite execution with interval time like setTimeout, but setTimeout will execute once but not setInterval. we can control setInterval by using clearInteval(--inteval timer variable--).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.