Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!