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

241
Views
Cómo llamar a 4 funciones cada 14 días

SetInterval no funcionará aquí porque necesito que esté limitado por fecha/hora. Quiero que las funciones se llamen en una fecha determinada y luego se alternen cada 2 semanas, independientemente de la actualización de la página.

La precisión no es tan importante, por lo que no importa si se usa el reloj del usuario.

 let div1 = document.querySelector(".div1"); let div2 = document.querySelector(".div2"); let div3 = document.querySelector(".div3"); let div4 = document.querySelector(".div4"); function function1() { div1.style.display = "block"; div2.style.display = "none"; div3.style.display = "none"; div4.style.display = "none"; } function function2() { div1.style.display = "none"; div2.style.display = "block"; div3.style.display = "none"; div4.style.display = "none"; } function function3() { div1.style.display = "none"; div2.style.display = "none"; div3.style.display = "block"; div4.style.display = "none"; } function function4() { div1.style.display = "none"; div2.style.display = "none"; div3.style.display = "none"; div4.style.display = "block"; }
 <div class="div1"> 1 </div> <div class="div2"> 2 </div> <div class="div3"> 3 </div> <div class="div4"> 4 </div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Sé que dijiste que no quieres usar setInterval, pero es necesario aquí si quieres hacer esto sin bibliotecas. Afortunadamente, tengo la sensación de que quiso decir setInterval con 14 días como intervalo, y eso no es necesario. Esto es lo que haría (lo siento, no puedo probarlo antes de responder porque eso requeriría alrededor de un mes):

 const startDate = new Date(x); //x is just whenever you want to start this: if you //don't want to use a backend with a database, you'll need to hardcode this in the javascript window.setInterval(()=>{ let date = new Date(); let twoWeeks = 1.2096e+9 //2 weeks in milliseconds switch (Math.floor(((date - startDate)/twoWeeks)%4)) { //difference between the dates, divide it by the twoWeeks number to get how many of //that interval have passed, get the remainder from dividing by 4 in case more than //8 weeks have passed, then floor it to get an integer case 1: function1(); break; case 2: function2(); break; case 3: function3(); break; case 0: //will happen if it's happened 4 times or 0 function4(); break; } }, y); //y is any interval in milliseconds, shorter will be more precise but will use more memory

si desea evitar que ejecute la function4() antes de que pase el primer intervalo de 2 semanas, puede actualizar la función de la siguiente manera:

 window.setInterval(()=>{ let date = new Date(); let twoWeeks = 1.2096e+9; if ((date - startDate)/twoWeeks < 4) return; switch (Math.floor(((date - startDate)/twoWeeks)%4)) { case 1: function1(); break; case 2: function2(); break; case 3: function3(); break; case 0: function4(); break; } }, y);

Tenga en cuenta que esto hará que las funciones se llamen más de una vez dentro de ese intervalo, pero teniendo en cuenta que solo se trata de establecer qué div será visible, eso no será un problema.

about 4 years ago · Juan Pablo Isaza Report

0

Una forma de solucionar esto es usando cron .

Los documentos en ese enlace lo explican bien, pero esencialmente puede establecer una fecha y hora y pasar la función que desea llamar en función de los parámetros de fecha/hora dados

 var CronJob = require('cron').CronJob; var job = new CronJob('* * * * * *', function() { console.log('You will see this message every second'); }, null, true, 'America/Los_Angeles'); job.start();

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