Home > other >  Get variable value from a function to other function javascript
Get variable value from a function to other function javascript

Time:01-26

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)

CodePudding user response:

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)}

CodePudding user response:

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)

CodePudding user response:

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>

  •  Tags:  
  • Related