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

169
Views
¿Cómo activar la solicitud AJAX periódicamente?
<meta http-equiv="Refresh" Content="5">

Este script recarga o actualiza la página cada 5 segundos. Pero quiero hacerlo usando jQuery y la llamada AJAX. ¿Es posible?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Como otros han señalado, setInterval y setTimeout harán el truco. Quería resaltar una técnica un poco más avanzada que aprendí de este excelente video de Paul Irish: http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/

Para tareas periódicas que pueden terminar tomando más tiempo que el intervalo de repetición (como una solicitud HTTP en una conexión lenta), es mejor no usar setInterval() . Si la primera solicitud no se ha completado y comienza otra, podría terminar en una situación en la que tiene varias solicitudes que consumen recursos compartidos y se mueren de hambre entre sí. Puede evitar este problema esperando para programar la siguiente solicitud hasta que se complete la última:

 // Use a named immediately-invoked function expression. (function worker() { $.get('ajax/test.html', function(data) { // Now that we've completed the request schedule the next one. $('.result').html(data); setTimeout(worker, 5000); }); })();

Para simplificar, utilicé la devolución de llamada exitosa para la programación. La desventaja de esto es que una solicitud fallida detendrá las actualizaciones. Para evitar esto, podría usar la devolución de llamada completa en su lugar:

 (function worker() { $.ajax({ url: 'ajax/test.html', success: function(data) { $('.result').html(data); }, complete: function() { // Schedule the next request when the current one's complete setTimeout(worker, 5000); } }); })();
over 4 years ago · Santiago Trujillo Report

0

Sí, puede usar el método setTimeout( setTimeout() de JavaScript o el método setInterval() para invocar el código que le gustaría ejecutar. Así es como puede hacerlo con setTimeout:

 function executeQuery() { $.ajax({ url: 'url/path/here', success: function(data) { // do something with the return value here if you like } }); setTimeout(executeQuery, 5000); // you could choose not to continue on failure... } $(document).ready(function() { // run the first time; all subsequent calls will take care of themselves setTimeout(executeQuery, 5000); });
over 4 years ago · Santiago Trujillo Report

0

Puede usar setTimeout o setInterval .

La diferencia es que setTimeout activa su función solo una vez y luego debe configurarla nuevamente. setInterval sigue activando la expresión una y otra vez, a menos que le diga que se detenga

over 4 years ago · Santiago Trujillo Report
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!