0

It's kinda hard to explain for me, but I'll give an example.

Let's take this function:

$(window).resize(()=>{
    if(window.innerHeight < 500){
        $wrapper.height('100vh')
    }
    else{
        $wrapper.height('60vh')
    }
})

Of course this code doesn't execute without resizing the window, so I have to write it again outside the resize function, so it works all the time, just like that:

if(window.innerHeight < 500){
    $wrapper.height('100vh')
}
else{
    $wrapper.height('60vh')
}

Is there a way not to repeat the code like in the example above? Is there a function for this?

1 Answer 1

2

Write the callback you pass to .resize outside, once. Then, call it, and also pass it as a callback:

const handleResize = () => {
  if(window.innerHeight < 500){
      $wrapper.height('100vh')
  }
  else{
      $wrapper.height('60vh')
  }
};
handleResize();
$(window).resize(handleResize);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.