Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Problemi con clearInterval() => browser in ciclo infinito

    Ciao a tutti,
    ho un problema con clearInterval() che non riesco a risolvere.. cioè in realtà con capisco a cosa sia dovuto.

    Nel codice sottostante setto degli intervalli con alcune funzioni lanciate dall'evento onmouseover. Con l'evento ounmouseout DOVREI cancellare gli intervalli.

    Il problema però è che gli intervalli non si cancellano, le funzioni continuano la loro esecuzione all'infinito e il browser si impalla.

    Qualche idea?

    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="imagetoolbar" content="no" />
    <meta name="author" content="Sergi Meseguer, meddle" />
    <title>scroll</title>
    
    <style type="text/css">
    	#scroll_container{
    		position:relative;
    		width:200px;
    		height:200px;
    		border:2px solid red;
    		overflow:hidden;
    	}
    
    	#scroll_content{
    		position:absolute;
    		top:0px;
    		left:0px;
    		margin:0px;
    		padding:0px;
    		border:2px solid black;
    	}
    </style>
    
    <script type="text/javascript">
    	var timer = null;
    	var delay = 10;
    	var speed = 5;
    	var currentX = 0;
    	var currentY = 0;
    	var containerWidth = 0;
    	var containerHeight = 0;
    	var contentWidth = 0;
    	var contentHeight = 0;
    	var offsetX = 0;
    	var offsetY = 0;
    
    	function initializeScroll(container,content) {
    		containerWidth = container.clientWidth;
    		containerHeight = container.clientHeight;
    		contentWidth = content.clientWidth;
    		contentHeight = content.clientHeight;
    		offsetX = contentWidth-containerWidth;
    		offsetY = contentHeight-containerHeight;
    	}
    
      function moveUp(obj){
    		targetObj = obj;
    		if ( currentY >= 0 ) {
    			clearTimer();
    			return true;
    		}
    		currentY = currentY + speed;
    		targetObj.style.top = currentY+'px';
    		timer = setInterval('moveUp(targetObj)',delay);
    	}
    
    	function moveDown(obj){
    		targetObj = obj;
    		if ( offsetY < 0 ||  Math.abs(offsetY) < Math.abs(currentY) ) {
    			clearTimer();
    			return true;
    		}
    		currentY = currentY - speed;
    		targetObj.style.top = currentY+'px';
    		timer = setInterval('moveDown(targetObj)',delay);
    	}
    
    	function moveLeft(obj){
    		targetObj = obj;
    		if ( currentX >= 0 ) {
    			clearTimer();
    			return true;
    		}
    		currentX = currentX + speed;
    		targetObj.style.left = currentX+'px';
    		timer = setInterval('moveleft(targetObj)',delay);
    	}
    
    	function moveRight(obj){
    		targetObj = obj;
    		if ( offsetX < 0 ||  Math.abs(offsetX) < Math.abs(currentX) ) {
    			clearTimer();
    			return true;
    		}
    		currentX = currentX - speed;
    		targetObj.style.left = currentX+'px';
    		timer = setInterval('moveRight(targetObj)',delay);
    	}
    
    	function clearTimer(){
    		if (timer)
    			timer = clearInterval(timer);
    	}
    
    	window.onload = function() {
    		initializeScroll(document.getElementById('scroll_container'),document.getElementById('scroll_content'));
    	}
    </script>
    
    </head>
    
    <body>
    
    <div id="scroll_container">
    	<div id="scroll_content">
    		
    
    riga</p>
    		
    
    riga</p>
    		
    
    rigaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
    		
    
    riga</p>
    		
    
    riga</p>
    		
    
    riga</p>
    	</div>
    </div>
    
    Su
    Giù
    Sx
    Dx
    
    <div id="debug">x</div>
    
    </body>
    </html>
    Grazie in anticipo!

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    ad un' occhiata veloce, ti basta sostituire tutti i setInterval con setTimeout (e ovviamente pulire con clearTimeout invece che clearInterval)
    ciao

  3. #3
    Originariamente inviato da Xinod
    ad un' occhiata veloce, ti basta sostituire tutti i setInterval con setTimeout (e ovviamente pulire con clearTimeout invece che clearInterval)
    ciao
    ad un'altra occhiata veloce
    questo
    Codice PHP:
    timer setInterval('moveRight(targetObj)',delay); 
    non si può fare


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4

  5. #5
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Per chi volesse riutilizzarlo: c'e' una maiuscola di meno alla riga

    timer = setTimeout('moveLeft(targetObj)',delay);

    ciao
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.