Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

jQuery timer jquery

  • Please log in to reply
3 replies to this topic

#1 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 20 December 2011 - 02:18 PM

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?
  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 20 December 2011 - 08:19 PM

Aren't you supposed to set the onClick attribute of the input element?

* * *

...

...
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);

  • 0

#3 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 21 December 2011 - 02:13 AM

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/
  • 0

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 21 December 2011 - 11:57 AM

...

...
$('#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.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download