Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
Array is updated before it should

I'm really confused about this one.

This is about saving html canvas drawings to the database. I have two variables:

let canvasesData = [];
let savedCanvasesData = [];

canvasesData contains the new data and savedCanvasesData contains the data which has been saved on the server. The first time this code runs every thing works fine, but when it runs for the second time (or more) then things get strange.

$(document).on('click', '#saveDrawings', function(){
    console.log(canvasesData); // This is identical to canvasesData after the update (3 lines down,) why?
    console.log(savedCanvasesData); // This is identical to canvasesData at the second run, how is this possible?
    canvasesData[currentCanvas] = {'img': canvas.toDataURL('image/jpeg', 1), 'txt': $('#drawingDescr').val()};
    console.log(canvasesData);
    if(!arrayIsEqual(canvasesData, savedCanvasesData)){
        //This doesn't run the second time, because the arrays are identical, which shouldn't be so.
        let toSave = canvasesData;
        data = {'imgs': toSave};
        $.post('/APIUrl', data, function(response){
            // The only explanation I have is that this is the first thing that runs
            //  when the button is pressed. But when I console.log() the following line
            //  it tells me it runs only after the response, like it should.
            savedCanvasesData = canvasesData;
        });
    }
});

Nowhere else do I use the savedCanvasesData array. I do use the canvasesData array in other places but these function are not called in this process, so it can't be changed. I'm really confused as to how this can be updated before the onClick code even runs.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda