Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery plugin callback and ‘this’ Re: jQuery plugin callback and ‘this’

#131069
CrocoDillon
Participant

Okay clearing the timeout has no use because the timeout already fired if you’re inside the checkNumber function, that’s why that doesn’t work. What you want to do is return from the function after calling the `onFinished`

if (typeof settings.onFinished == ‘function’) {
settings.onFinished();
}
return;

remove the `clearTimeout` as it really does nothing (you might as well remove the `timer` variable), and add `var` keywords to your `offset` and `current_number` variables to keep them in function scope.

EDIT:

Actually that brings another problem, if the count to isn’t dividable by offset (for example count from 0 to any uneven number, with jump = 2), because this never gets printed:

if(current_number >= settings.countTo) {
current_number = settings.countTo;

Solution is to do the counting up before that if:

current_number += offset;
if(current_number >= settings.countTo) {
current_number = settings.countTo;

instead of inside the else where you create the timeout.