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

133
Views
Uso de datos de WebSocket fuera del evento onmessage

Hola, he estado jugando con el aprendizaje de JavaScript en mi tiempo libre y tenía una pregunta rápida. Estoy usando websockets para ver los precios de las criptomonedas como se muestra en el siguiente código:

 let ethws = new WebSocket('wss://stream.binance.com:9443/ws/ethusdt@trade'); let btcws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@trade'); let ethstockPriceElement = document.getElementById('eth-stock-price') let btcstockPriceElement = document.getElementById('btc-stock-price') let ethlastPrice = null; let btclastPrice = null; ethws.onmessage = (event) => { let ethstockObject = JSON.parse(event.data) let ethprice = parseFloat(ethstockObject.p).toFixed(2) ethstockPriceElement.innerText = ethprice ethstockPriceElement.style.color = !ethlastPrice || ethlastPrice === ethprice ? 'black' : ethprice > ethlastPrice ? 'green' : 'red'; ethlastPrice = ethprice } btcws.onmessage = (event) => { let btcstockObject = JSON.parse(event.data) let btcprice = parseFloat(btcstockObject.p).toFixed(2) btcstockPriceElement.innerText = btcprice btcstockPriceElement.style.color = !btclastPrice || btclastPrice === btcprice ? 'black' : btcprice > btclastPrice ? 'green' : 'red'; btclastPrice = btcprice }

Quería usar empujar el precio a una variable de "actualización" fuera del evento para poder hacer algo como 2 btc precio 3 eth precio luego btcprice + ethprice para obtener un precio total de actualización en vivo de 2 btc y 3th. Sin embargo, no puedo usar los datos fuera del evento. ¿Cuál sería la mejor manera de lograr esto?

about 4 years ago · Juan Pablo Isaza
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!