0

I got a simple socket.io function, and I can print datas to ejs view. But I would like to use the data also for a variable outside the function. But have no idea...

var data_ras_height = "";

socket.on('ras_height', function (data_ras_height) {
  document.getElementById("ras_height_text").textContent = data_ras_height;
  console.log(data_ras_height); // This prints values
});
console.log(data_ras_height); // This prints nothing
Paolovip
  • 123
  • 2
  • 10
  • 1
    Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. It "prints nothing" because it prints immediately after setting the `socket.on` handler, whereas it's *set* at an indeterminate time in the future by the async message handler. – Dave Newton Oct 25 '21 at 16:00
  • Thank you, and sorry for duplicated post – Paolovip Oct 26 '21 at 08:47

0 Answers0