Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Get variable value from a function to other function javascript

I want to get value from a function to another function . but i am new at javascript, can someone hlep me solve thi. i want to call the value of dinoRight, dinoLeft, and the other to cactus1move function.

const spawn = () => {
    const dinoRight = document.getElementById('dino').getBoundingClientRect().left + document.getElementById('dino').getBoundingClientRect().width;
    const dinoLeft = document.getElementById('dino').getBoundingClientRect().left; 
    const dinoHeight = document.getElementById('dino').getBoundingClientRect().height;
    const cactusTop = document.getElementById('cactus').getBoundingClientRect().top;
    const cactusWidth = document.getElementById('cactus').getBoundingClientRect().width;
    const containerLeft = document.getElementById('Container').getBoundingClientRect().left;
    cactus1move();
}
const cactus1move = setInterval(()=>{
    if (cactusTop<=dinoBottom&&dinoRight>=cactusLeft&&cactusRight>=dinoLeft) {
        ...
    } else if (cactusLeft<=containerLeft+10&&cactusLeft>=containerLeft){
        ...
    } else {
        ...
    }
},20)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const spawn = () => {
    const dinoRight = document.getElementById('dino').getBoundingClientRect().left + document.getElementById('dino').getBoundingClientRect().width;
    const dinoLeft = document.getElementById('dino').getBoundingClientRect().left; 
    const dinoHeight = document.getElementById('dino').getBoundingClientRect().height;
    const cactusTop = document.getElementById('cactus').getBoundingClientRect().top;
    const cactusWidth = document.getElementById('cactus').getBoundingClientRect().width;
    const containerLeft = document.getElementById('Container').getBoundingClientRect().left;
    cactus1move(dinoRight, dinoLeft);
}


//not sure if you can pass it directly to the interval
    const cactus1move = setInterval((dino_right, dino_left)=>{
        if (cactusTop<=dinoBottom&&dinoRight>=cactusLeft&&cactusRight>=dinoLeft) {
            ...
        } else if (cactusLeft<=containerLeft+10&&cactusLeft>=containerLeft){
            ...
        } else {
            ...
        }
    },20)


//if it does not work you can pass the parameters to a function and then down to the interval
    const cactus2move = (dino_right, dino_left)=> {
        setInterval((dino_right, dino_left)=>{
        if (cactusTop<=dinoBottom&&dinoRight>=cactusLeft&&cactusRight>=dinoLeft) {
            ...
        } else if (cactusLeft<=containerLeft+10&&cactusLeft>=containerLeft){
            ...
        } else {
            ...
        }
    },20)}
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your code, variables are declared inside the functions (i.e the scope of these variables is restricted to function). You can declare these variables outside the functions so that both the functions will have access to it. Your code will go something like:

    let dinoRight, dinoLeft, dinoHeight, cactusTop, cactusWidth, containerLeft;
const spawn = () => {
        dinoRight = document.getElementById('dino').getBoundingClientRect().left + document.getElementById('dino').getBoundingClientRect().width;
        dinoLeft = document.getElementById('dino').getBoundingClientRect().left; 
        dinoHeight = document.getElementById('dino').getBoundingClientRect().height;
        cactusTop = document.getElementById('cactus').getBoundingClientRect().top;
        cactusWidth = document.getElementById('cactus').getBoundingClientRect().width;
        containerLeft = document.getElementById('Container').getBoundingClientRect().left;
        cactus1move();
    }
    const cactus1move = setInterval(()=>{
        if (cactusTop<=dinoBottom&&dinoRight>=cactusLeft&&cactusRight>=dinoLeft) {
            ...
        } else if (cactusLeft<=containerLeft+10&&cactusLeft>=containerLeft){
            ...
        } else {
            ...
        }
    },20)
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can pass parameters when calling the method, so that it can be used inside the method. If it is not necessary, I recommend not to use global variables to get the value.

In the following code, I call cactus1move passing the value and wrap the value into an object. When implemented inside cactus1move, I use destructuring assignment to get the value out of the object, and then you can use these values to do something.

const spawn = () => {
  const dinoRight = document.getElementById('dino').getBoundingClientRect().left + document.getElementById('dino').getBoundingClientRect().width;
  const dinoLeft = document.getElementById('dino').getBoundingClientRect().left;
  const dinoHeight = document.getElementById('dino').getBoundingClientRect().height;
  const cactusTop = document.getElementById('cactus').getBoundingClientRect().top;
  const cactusWidth = document.getElementById('cactus').getBoundingClientRect().width;
  const containerLeft = document.getElementById('Container').getBoundingClientRect().left;
  cactus1move({
    dinoRight,
    dinoLeft,
    dinoHeight,
    cactusTop,
    cactusWidth,
    containerLeft
  });
}

const cactus1move = (data) => {
  return setInterval(() => {
    const {
      dinoRight,
      dinoLeft,
      dinoHeight,
      cactusTop,
      cactusWidth,
      containerLeft
    } = data;
    console.log('dinoRight', dinoRight);
    console.log('dinoLeft', dinoLeft);
    console.log('dinoHeight', dinoHeight);
    console.log('cactusTop', cactusTop);
    console.log('cactusWidth', cactusWidth);
    console.log('containerLeft', containerLeft);
  }, 2000);
}
#dino {
  width: 80px;
  height: 80px;
  background-color: #FFBB73;
  float: left;
}

#cactus {
  width: 90px;
  height: 90px;
  background-color: #FFBB33;
  float: left;
}

#Container {
  width: 100px;
  height: 100px;
  background-color: #FFBB13;
  float: left;
}
<div id='dino'></div>
<div id='cactus'></div>
<div id='Container'></div>
<button onclick="spawn()">spawn</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda