Problem with changing setInterval | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem with changing setInterval

*unneccessary story* I've made a whatsApp bot that will send a message every 30 minutes, but since I have it for a challenge with a friend I wanted to make it go every half hour - previous interval newInterval = 1800000-previousInterval // to calc the difference +1800000 //for an extra half hour + difference + Math.round(Math.random()*600000) - 300000 //random number between -5 and 5 minutes and the interval is like this setInterval(function(){},nieuwInterval); How can I make the interval changing? (btw Idc if I have to rewrite it like setInterval(send(),nieuwInterval); function send(){}

8th Oct 2018, 2:14 PM
Roel
Roel - avatar
2 Answers
+ 2
if you want current interval timer run in new interval, you got to clear current interval timer first, and then set with new interval timer. If interval changed in every timer call, better way is using setTimeout and call another new setTimeout in every timer exist.
8th Oct 2018, 3:24 PM
Calviղ
Calviղ - avatar
0
Calviղ I didn't really know how but I tried this (sorry if it's completely wrong, I'm just a beginner if it is about intervals) setInterval(function(){ ... clearInterval() newInterval() },x); function newInterval(){ ... x = ... }
8th Oct 2018, 4:07 PM
Roel
Roel - avatar