Niestety z jakiegoś niewiadomego mi powodu wewnętrzna metoda sprawdzająca nie wyłącza mi licznika. Z zewnątrz mogę to zrobić, ale z wew. klasy nie.
Kod
function Counter()
{
this.jump = 1;
this.meter = 0;
this.timeout = 100;
this.count_to = null;
this.evt_func = null;
this.task_pointer = null;
var ths = this
this.start = function()
{
this.meter += this.jump;
if(this.evt_func !== null)
{
setTimeout(this.evt_func, 0);
}
if(this.count_to !== null && this.count_to == this.meter)
{
this.stop();
}
this.task_pointer = setTimeout(function(){ ths.start(); }, this.timeout);
}
this.stop = function()
{
$('span#test2').html(this.meter);
clearTimeout(this.task_pointer);
}
this.eventRegister = function($fn)
{
if(!jQuery.isFunction($fn)) {
alert('Paremeter incorrect given for Counter::eventRegister');
} else {
this.evt_func = $fn;
}
}
}
$Counter = new Counter();
$Counter.meter = 0;
$Counter.count_to = 20;
$Counter.eventRegister(function($Counter)
{
$('span#test').html($Counter.meter);
});
$Counter.start();
{
this.jump = 1;
this.meter = 0;
this.timeout = 100;
this.count_to = null;
this.evt_func = null;
this.task_pointer = null;
var ths = this
this.start = function()
{
this.meter += this.jump;
if(this.evt_func !== null)
{
setTimeout(this.evt_func, 0);
}
if(this.count_to !== null && this.count_to == this.meter)
{
this.stop();
}
this.task_pointer = setTimeout(function(){ ths.start(); }, this.timeout);
}
this.stop = function()
{
$('span#test2').html(this.meter);
clearTimeout(this.task_pointer);
}
this.eventRegister = function($fn)
{
if(!jQuery.isFunction($fn)) {
alert('Paremeter incorrect given for Counter::eventRegister');
} else {
this.evt_func = $fn;
}
}
}
$Counter = new Counter();
$Counter.meter = 0;
$Counter.count_to = 20;
$Counter.eventRegister(function($Counter)
{
$('span#test').html($Counter.meter);
});
$Counter.start();
$Counter.meter może nie działać bo kod przepisałem z mojej klasy obsługi wywołania AJAX która jest statyczna zmieniając function() na function($Counter) więc nie jestem pewien czy ten przykład będzie śmigać tak jak powinien. Ale u mnie $('span#test') zmienia swoją wartość co sekundę. Natomiast $('span#test2') gdy licznik dojdzie do 20. Niestety nie zatrzymuje się na nich tylko jedzie dalej do usranej śmierci... Z kolei jeśli wywołam metodę stop z zewnątrz klasy to licznik przestaje dalej lecieć. Co może być przyczyną?
p.s.
Tak przy okazji się zapytam. Wiecie może jak wywołanie 'zdarzenia licznika' setTimeout(this.evt_func, 0); zastąpić czym nie wykorzystującym setTimeout? Chodzi mi o to jak mam to w inny sposób wywołać.