Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda