Foros del Web » Programando para Internet » Javascript »

[Me trabé -.-] ¿Como paso un objeto con setInterval? :S

Estas en el tema de [Me trabé -.-] ¿Como paso un objeto con setInterval? :S en el foro de Javascript en Foros del Web. Bueno, estoy intentando hacer una función para mover una div, que al principio se mueva mucho y luego se mueva mas despacio hasta completar una ...
  #1 (permalink)  
Antiguo 02/09/2009, 11:08
 
Fecha de Ingreso: mayo-2008
Ubicación: En la guarida del mal, tratando de conquistar el mundo
Mensajes: 174
Antigüedad: 15 años, 10 meses
Puntos: 2
Pregunta [Me trabé -.-] ¿Como paso un objeto con setInterval? :S

Bueno, estoy intentando hacer una función para mover una div, que al principio se mueva mucho y luego se mueva mas despacio hasta completar una determinada cantidad de píxeles...

Código PHP:
<html>
    <
head>
        <
title>Prueba JS</title>
        <
script language="JavaScript">
            function 
move(objxa)
            {    
                
dist=parseInt(obj.style.left) - x;
                
sumat=sum(a);
                
window.a=a;;
                
window.inter=setInterval("move2("+obj.id+", "+dist+", "+sumat+")"33);
                
            }

            function 
move2(objid,dist,sumat)
            {
                
obj=document.getElementById(objid);

                if(
dist<0)
                    
obj.style.left=parseInt(obj.style.left)+(dist/sumat)*window.a+"px";
                else
                    
obj.style.left=parseInt(obj.style.left)-(dist/sumat)*window.a+"px";
                
                
window.a--;
                if(
a<0)
                    
clearInterval(window.inter);
            }

            function 
sum(num)
            {
                var 
result=0;
                for(
i=1;i<=num;i++) result+=i;
                return 
result;
            }
        
</script>
    </head>

    <body style="margin:0px;padding:0px;">
        <div id="lol" style="width:100px;height:100px;position:absolute;left:10px;background-color:#000;" onMouseOver="move(this, 800, 100)"></div>
    </body>
</html> 
No se porque, me da un Warnning.
El warnning dice lo que dice el título:

"Element refereced by ID/NAME in the global scope. Use W3C standard document.getElementBydId().instead"

Lo marca en la linea donde está el "setInterval"...
Tengo entendido que no se puede pasar un objeto por "setInterval" porque lo convierte a string... En ese caso, como miercoles hago para pasar un objeto?? :S

Última edición por ZequeZ; 02/09/2009 a las 12:12 Razón: Ups
  #2 (permalink)  
Antiguo 02/09/2009, 12:30
 
Fecha de Ingreso: mayo-2008
Ubicación: En la guarida del mal, tratando de conquistar el mundo
Mensajes: 174
Antigüedad: 15 años, 10 meses
Puntos: 2
De acuerdo Respuesta: [Me trabé -.-] ¿Como paso un objeto con setInterval? :S

Hago una neuva respuesta porque lo solucioné =D
Despues de leer un tutorial muy completo sobre setInterval y setTimeout, entendí porque no me andaba xD.

Aca está la solución: (Eso no quiere decir que el script ande como debería eh? xD)

Código PHP:
<html>
    <
head>
        <
title>Prueba JS</title>
        <
script language="JavaScript">
            function 
move(objxa)
            {    
                
dist=parseInt(obj.style.left) - x;
                
sumat=sum(a);
                
window.a=a;
                
window.inter=setInterval("move2(document.getElementById('"+obj.id+"'), "+dist+", "+sumat+")"1000);
                
            }

            function 
move2(obj,dist,sumat)
            {
                
alert(obj.id);
                if(
dist<0)
                    
obj.style.left=parseInt(obj.style.left)+(dist/sumat)*window.a+"px";
                else
                    
obj.style.left=parseInt(obj.style.left)-(dist/sumat)*window.a+"px";
                
                
window.a--;
                if(
a<0)
                    
clearInterval(window.inter);
            }

            function 
sum(num)
            {
                var 
result=0;
                for(
i=1;i<=num;i++) result+=i;
                return 
result;
            }
        
</script>
    </head>

    <body style="margin:0px;padding:0px;">
        <div id="lol" style="width:100px;height:100px;position:absolute;left:10px;background-color:#000;" onMouseOver="move(this, 800, 100)"></div>
    </body>
</html> 
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:57.