Jquery SetInterval help

  • steve101
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Mar 26, 2004
  • Posts: 35
  • Status: Offline

Post December 21st, 2010, 3:28 am

Hi,

I have a problem with setInterval (jquery). It works great, but I wan't the element to stop refreshing when a textarea is selected (so that users can insert text).

Here is the refresh code I'm using currently.

JAVASCRIPT Code: [ Select ]
 $(document).ready(function() {
   $("#container").load("php/functions.php?page=wall");
 
   refreshId = setInterval(function() {
      $("#container").load("php/functions.php?page=wall&r="+Math.random());
 
   
   }, 1000);
});
 
  1.  $(document).ready(function() {
  2.    $("#container").load("php/functions.php?page=wall");
  3.  
  4.    refreshId = setInterval(function() {
  5.       $("#container").load("php/functions.php?page=wall&r="+Math.random());
  6.  
  7.    
  8.    }, 1000);
  9. });
  10.  

How could I alter this, for example, to make the interval stop whilst the the text area is in focus/being typed in?

Many Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 21st, 2010, 3:28 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post December 22nd, 2010, 2:15 am

Declare "refreshId" as a global variable outside of the function so that future events can access it. Then attach a focus event handler to the container.

Code: [ Select ]
$('#container').focus(function(){
  clearInterval(refreshId);
});
  1. $('#container').focus(function(){
  2.   clearInterval(refreshId);
  3. });
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 222 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.