0

I have a simple setTimeout function that runs at a specific time and works fine:

var now = new Date();
var milliTillExec = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), 0, 0) - now;
if (milliTillExec < 59500) {
    milliTillExec += 59500; 
}
window.setTimeout(function(){
    console.log('at 59:500');
},milliTillExec); 

trying to add a function that runs every 300 milliseconds after the previous function fired, so I did like this :

 function runEvery300Milli(){
    var t = new Date();
    window.setInterval(function(){
        if((t.getMinutes===59 && t.getMilliseconds>499)||(t.getMinutes===0 && t.getMilliseconds<500)){
            console.log(t.getMinutes()+ ":"+t.getSeconds() + ":"+ t.getMilliseconds());
        }
    }, 300)(); 
}
var now = new Date();
var milliTillExec = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), 0, 0) - now;
if (milliTillExec < 59500) {
    milliTillExec += 59500; 
}
window.setTimeout(function(){
    console.log('at 59:500');
    runEvery300Milli();
},milliTillExec);

but I get the following error:

Uncaught TypeError: setInterval(...) is not a function
    at runEvery300Milli
4
  • 3
    your setinterval looks like setInterval(fn, 300)() ... since setInterval returns undefined, this is what is not a function - setInterval is, but what it returns is not - simply remove the () after , 300) Commented Oct 5, 2019 at 3:26
  • @Nick, I made a window.setInterval , that didn't work either Commented Oct 5, 2019 at 3:33
  • @Bravo you should post as answer mate not comment, as comment can vanish later on, :+1: Commented Oct 5, 2019 at 3:33
  • @Mostafa what Bravo explained in comment is exact reason for error, here's a Demo, Commented Oct 5, 2019 at 3:34

1 Answer 1

4

your setinterval looks like

setInterval(fn, 300)()

... since setInterval returns undefined, this is what is not a function, and the cause of the error

i.e. setInterval is a function, but what it returns is not - simply remove the () after , 300)

function runEvery300Milli(){
    var t = new Date();
    window.setInterval(function(){
        if((t.getMinutes===59 && t.getMilliseconds>499)||(t.getMinutes===0 && t.getMilliseconds<500)){
            console.log(t.getMinutes()+ ":"+t.getSeconds() + ":"+ t.getMilliseconds());
        }
    }, 300);  // <=== removed trailing ()
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can create a running snippet on SO, running snippet on SO, anyways +1
@CodeManiac - yes, but why in this case would a running snippet of part of the OP's code help at all? I see no benefit in producing a snippet of code for such a simple error/solution - for example, yesterday I did so, stackoverflow.com/questions/58230832/… and stackoverflow.com/questions/58229257/… to show the output was a required - so I'm well aware of the tools available on SO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.