Jump to content

jQuery/javascript setInterval & clearInterval, just explain me why it doesn't work?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Stasonix

Stasonix

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal
it is the simple setInterval, clearInterval functions, I did that when I clicked button DO it must be toggle class 'running' and start/stop timer, so, whats wrong? The Fiddle Code: Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)
source code:


function toLove() {

 $('div').append('<p>test</p>');    

}


function startDO() {

 var startID = setInterval(toLove(),1000);

}


function stopDO(){

  clearInterval(startID);  

}


$('#start').click(function(){

    if ($(this).hasClass('running')){

      $(this).removeClass('running');

     stopDO();  

    }

    else 

    {

     $(this).addClass('running');

     startDO();  

    }

});



<div>div block, watch below<br/>

    when you click button DO it's append < p >test < / > inside this.

</div>

<input type="button" value="do" id="start" />


when I clicked my DO button, only first time I got append p,next it's only after second time click, but the it must be automaticly, by setInterval function...? Maybe troubles with toggle?

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Aren't you supposed to set the onClick attribute of the input element?

* * *

Stasonix said:

...
...

var startID = setInterval(toLove(),1000);

...
...

NOoooo! That's not what you do. You're supposed to at least use some sort of reference to the function, not CALL the function right there and then. Try this:
var startID = setInterval ("toLove ();", 1000); 


#3
Stasonix

Stasonix

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal
Seems nothing good:

var startID;

function toLove() {

 $('div').append('<p>test</p>');    

}


function startDO() {

 startID = setInterval("toLove()",1000);

}


function stopDO(){

  clearInterval(startID);  

}


$('#start').click(function(){

    if ($(this).attr('name')=='running'){

      $(this).attr('name','');

     stopDO();  [URL="http://jsfiddle.net/Stasonix/6jvcx/2/"]

    }[/URL]

    else

    {

     $(this).attr('name','running');

     startDO();  

    }

});


it doesn't work.
http://jsfiddle.net/Stasonix/6jvcx/2/

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

Stasonix said:

...
...

$('#start').click(function(){

    if ($(this).attr('name')=='running'){

      $(this).attr('name','');

     stopDO();  

    }

    else

    {

     $(this).attr('name','running');

     startDO();  

    }

});
...

Wherever you have '$(this).attr(...' , try replacing '$(this)' with just 'this' ; I don't think you need the dollar sign with the parentheses in those cases. Though I'm not sure if that's the problem or not.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users