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

226
Views
¿El evento window.onbeforeunload se ejecuta antes de tiempo?

Así que estoy construyendo una sala de chat en tiempo real, y este es mi código hasta ahora. Estoy tratando de crear mensajes de salida, en el sentido de que cuando alguien cierra la pestaña o va a una página diferente, inserta un mensaje en la base de datos.

Esto es lo que tengo hasta ahora:

 <?php session_start(); require('./essentials.php'); welcome_message(0, 11, true); //custom function that I wrote in ./essentials.php ?> <html style="background-color:#202020;"> <head> <title>Programming - Parity Chatroom</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <script src='./socket.js'></script> <script type='text/javascript'> websocket.onopen = function() { alert('Connected to server!'); conn.send("Someone joined!"); } websocket.onmessage = function() { if (!document.hasFocus()) { var audio = new Audio("./notification.wav"); audio.play(); } } websocket.onclose = function() { setTimeout(function() { window.location.reload(); }, 5000); } window.onbeforeunload = function() { conn.close(1000, 'Closed page'); <?php exit_message(); /* function to insert exit message to db */ ?> } </script> </head> <FRAMESET cols="20%,*"> <FRAME src="sidebar.php" id='sidebar' style='overflow:hidden'> <FRAMESET rows="*,9%"> <FRAME src="messages.php" id='messages'> <FRAME src="newmessage.php" id='newmessage' style='overflow:hidden'> </FRAMESET> </FRAMESET> </html>

Entonces, el código funciona bien, pero creo que hay un problema con el orden en que se ejecuta el código. Específicamente, el evento javascript onbeforeunload parece activarse cada vez que visito la página. Sospecho que esto tiene que ver con el hecho de que el código PHP se carga y ejecuta cuando se carga la página, considerando que es un lenguaje del lado del servidor.
¿Hay alguna manera de reducir la velocidad y hacer que el script se ejecute en orden?


¡Cualquier ayuda sería muy apreciada!

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!