2010-09-14 20 views
0

i ai ce code javascript:Howto sortie fonction setInterval défini dans jquery

 var step = 8;     // How many pixels to move per step 
     var current = -1920;   // The current pixel row 
     var imageWidth = 1920;  // Background image width 
     var headerWidth = 960;  // How wide the header is. 

     function slide_in(){ 
      //Go to next pixel row. 
      current += step; 

      //Set the CSS of the header. 
      $('#bgfade').css("background-position",current+"px 0"); 

      if(current==0) alert('stop'); 
     } 

     //Calls the scrolling function repeatedly 
     $('#bgfade').click(function(){   
      var init = setInterval("slide_in()", 1); 
     }); 

ce qui rend la lame de fond. Je veux quitter lorsque le courant var est = 0, et laisser l'arrière-plan dans cette position.

grâce

Répondre

0

Vous devez utiliser clearInterval(), comme ceci:

var step = 8;     // How many pixels to move per step 
var current = -1920;   // The current pixel row 
var imageWidth = 1920;  // Background image width 
var headerWidth = 960;  // How wide the header is. 
var init; 

function slide_in(){ 
    //Go to next pixel row. 
    current += step; 

    //Set the CSS of the header. 
    $('#bgfade').css("background-position",current+"px 0"); 

    if(current==0) clearInterval(init); 
} 

//Calls the scrolling function repeatedly 
$('#bgfade').click(function(){   
    init = setInterval(slide_in, 1); 
}); 

Les autres changements ici sont la portée, de sorte que l'ID de minuterie est disponible à l'autre fonction, ainsi que ne pas passer un chaîne à setInterval() pour éviter tout autre problème potentiel.

+0

merci, cela fonctionne mais je ne peux pas relier une autre action dans la séquence? Je clone la fonction pour faire le contraire mais ils ne fonctionnent pas l'un après l'autre – Mike

+0

@TrustWeb - Je ne suis pas, pouvez-vous expliquer ce commentaire un peu plus ?, qu'est-ce que vous essayez de faire? –

+0

ok, désolé. Je veux slide_in, effectuer des actions et slide_out. J'appelle la deuxième fonction comme la première mais la première manque. init = setInterval (slide_in, 1); alerte ('salut'); outinit = setInterval (slide_out, 1); – Mike