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

192
Views
How can I make variable save only the first time the condition is true

I have a variable that gets the height of the scrollbar when overflow-x-auto and I am using a svelte javascript framework.

for solving this issue I used this code:

<script>
  let hWith = 0;
  let hNot = 0;

  $: scrollBarHeight = hWith - hNot;
</script>

<div 
  bind:offsetHeight={hWith} 
  bind:clientHeight={hNot}
>
  my content...
</div>

and is working fine and outputs: 17

the problem is that bind: will always fire at every rerender.

As you know a scrollbar will not magically change the size, so it is not important to recalculate always the value of height

once we get the 17 value (that can be different depending on the browser), stop recalculating.


and where is the problem?

the problem is that the variable need to be reactive with $:
at the start,
because since the container will not overflow this means is 0

but once we get a number greater than 0. (returnedValue > 0)
the variable doesn't need to be reactive anymore


I basically want that when I save the scrollBarHeight (a simple calculation hWith - hNot), I will not calculate it anymore.

you know I don't like the fact that I can't write directly the formula inside one variable instead of 3.

with bind: it will always set the variable with the new value. (that can be inefficient since I the value don't change a lot)

❌

let hWith = 0;
let hNot = 0;

$: scrollBarHeight = hWith - hNot;

✅ something like this.

$: scrollBarHeight = if (div.offsetHeight - div.clientHeight > 0) {  
   // logic/code to make this variable not reactive anymore
   return div.offsetHeight - div.clientHeight;
}

why do I need it?

this logic is a child, not the body. so this is why something is overflowing. and I need that value for correcting some bugs.

the problem the child changes a lot and rerenders and this maybe can rerender and also bind the values a lot.

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can just declare a regular variable and add a reactive statement without immediate assignment. E.g.

let scrollBarHeight = 0;

$: if (scrollBarHeight == 0 &&
       div.offsetHeight - div.clientHeight > 0) {
   scrollBarHeight = div.offsetHeight - div.clientHeight;
}
almost 4 years ago · Santiago Trujillo Report

0

Breaking reactive statement early might help

let counter = 0;
let evens = 0;
let threes = 0;

$: {
    if (counter < 10) {
        break $;
    }
    if (counter % 2 === 0) {
        evens++;
    }
}

$: {
    if (counter < 10) {
        break $;
    }
    if (counter % 3 === 0) {
        threes++;
    }
}

source repl: https://svelte.dev/repl/be829d83edda4dd882b3b375f889745c?version=3.44.0

almost 4 years ago · Santiago Trujillo 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!