Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > JavaScript: Общие вопросы > Не работает setInterval в обьекте


Автор: Хоббит 3.8.2006, 17:39
Код

function Scroll_moveForvard()
{
  if (count < scrollValue)
  {         
    speed = (scrollValue - count) / this.kStop + 1;
    this.position = this.position - speed;        
        
    object.style.left = this.position + 'px';            
    count = count + speed;
  }
  else
  {        
    clearInterval(this.timer);
    this.timer = 0;
  }
}

function Scroll_initMoveForvard()
{ 
  if (this.timer == 0)
  {
    if (this.visibleElement < this.elementCount)
    {
      scrollValue = this.scrollWidth;
      this.visibleElement = this.visibleElement + 1;               
      count = 0;

      object = parentdoc.getElementById(this.scrollBlock);      
      
      this.timer = setInterval(this.moveForvard(), this.timeOut);
    }
  }
}    

var object;
var count = 0;
var scrollValue;
var parentdoc;

  
function Scroll(doc, scrollBlock, elementCount, width)
{
  parentdoc = doc;
  
  this.scrollBlock = scrollBlock;
  
  this.position = 0;
  this.scrollWidth = width;          
  
  this.timer = 0;
    
  this.kStop = 2.5;
  this.timeOut = 50;

  this.elementCount = elementCount;
  this.visibleElement = 1;
  
  this.moveBack = Scroll_moveBack;
  this.moveForvard = Scroll_moveForvard;
  this.initMoveBack = Scroll_initMoveBack;
  this.initMoveForvard = Scroll_initMoveForvard;
/*  this.initMoveTo = Scroll_initMoveTo;
  this.controlBackHover = Scroll_controlBackHover;
  this.controlBackOut = Scroll_controlBackOut;
  this.controlForvardHover = Scroll_controllForvardHover;
  this.controlForvardOut = Scroll_controlForvardOut;*/
}


Так вот по initMoveForvard функция запускается один раз хотя там стоит setInterval все условия внутри метода выполняются ... 

Автор: 12345c 3.8.2006, 18:29
Надо писать
this.timer = setInterval("this.moveForvard()", this.timeOut);
или
this.timer = setInterval(this.moveForvard, this.timeOut);
если интересует выполнение функции, а не её знчения smile (которое null)

Автор: vasac 3.8.2006, 20:59
Вообще-то, если нужно выполнение функции в контексте того же объекта, то придется немного поизвращаться:
Код

t = this;
this.timer = setInterval( ( function() { return t.moveForvard(); } ), this.timeOut );

Автор: Хоббит 4.8.2006, 10:52
спасибо за точный ответ... недавно учу javascript так что такие мелочи пока не всосал

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)