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

63
Visualizações
How-to send a variable to an async jQuery function and make shure the variable isn't changed after?

I have this code that change the key variable for each key in a dict. The purpose is to update html dom with the value of this key. What i want is to add a fadeIn and fadeOut transition on value update. the problem is that currently, since the .fadeOut() function seems async, the key got changed before the fadeOut() is finised so when i change the value after the fadeOut, the key is something else and the value i update are wrong. here is the exemple!

$(document).ready(function () {
  var devices = {};
  devices.a = 10;
  devices.b = 22;
  devices.c = 440;
  devices.d = 435;
  devices.e = 56475;
  devices.h = "dfgfsd";

  var fadeTime = 1000;
  for (key of Object.keys(devices)) {
    console.log(key + " | " + devices[key]);
    dom = document.getElementById(key);
    if(dom != undefined){
      $( dom ).fadeOut( fadeTime, function() {
        $( this ).text(devices[key]).fadeIn(fadeTime);
      });
    }
  }
});

https://jsfiddle.net/5zr8ts9f/15/

You can see there that all the field get updated with the last value of the dict!

Is there a way to detach of the variable we send to the jQuery function ??

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I final have found a way to do it!!

$(document).ready(function () {
  var devices = {};
  devices.a = 10;
  devices.b = 22;
  devices.c = 440;
  devices.d = 435;
  devices.e = 56475;
  devices.h = "dfgfsd";

  var fadeTime = 1000;
  for (key of Object.keys(devices)) {
    console.log(key + " | " + devices[key]);
    dom = document.getElementById(key);
    if(dom != undefined){
      updateField(dom, devices[key]);
    }
  }
});

function updateField(dom, value){
  $( dom ).fadeOut( fadeTime, function() {
    $( this ).text(value).fadeIn(fadeTime);
  });
}
about 4 years ago · Juan Pablo Isaza 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