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

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

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

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