Problems with setInterval function JavaScript - javascript

I'm using the setInterval function in JavaScript but I get like one hundred reps per second from the console.log('NOW').
setTimeout also has the same effect.
Where is my mistake?
I want to call the function "function1" every 15 minutes.
JavaScript Code:
function1() {
console.log('NOW');
.
.
.
});
},
refreshData() {
this.function1();
setInterval(this.refreshData(), 900000);
},
Thanks in adavance!

you're invoking the function
setInterval(this.refreshData(), 900000);
rather than passing a reference to a function
setInterval(this.refreshData, 900000);

Wrap your function call like This:
var self = this;
refreshData() {
this.function1();
setInterval(function(){ self.refreshData() }, 900000);
}

There are two possible ways:
In provided code you should use setTimeout, because you restart function manually:
function function1() {
console.log('NOW');
}
function refreshData() {
this.function1();
setTimeout(this.refreshData, 3000);
}
refreshData();
Or simply replace existing logic with setInterval, it should do all the job you've implemented manually:
function function1() {
console.log('NOW');
}
this.function1();
setInterval(this.function1, 3000);

Related

Javascript - Disable all nested functions for some time after one function function has been executed

After a function has run I want to disable a whole set of functions can be executed again. I thought the most logical way would be to put them all together inside an overarching function (in this case function run) and disable that function for some time.
So for example in the code below, when function exampleOne is executed I want to disable the whole run function for X time before it can run again.
function run(){
function exampleOne(){
// Code
sleep(1000);
}
function exampleTwo(){
// Code
sleep(1000);
}
}
run();
Take a look at underscore.js and especially the throttle or debounce functions.
You can define your functions as variables then you can disable them by assigning an empty or error functions to these variables. Two functions (e.g. enable/disable) can be responsible to disable/enable the whole set of functions. and of course you can use setTimeout to manage the delay.
for example:
var exampleOne, exampleTwo;
function enable(){
exampleOne = function(){
// do something
disable();
setTimeout(function() { enable(); }, 10000);
}
exampleTwo = function(){
// do something
disable();
setTimeout(function() { enable(); }, 10000);
}
}
function disable(){
exampleOne = function(){
Alert("i'm sleep!")
}
exampleTwo = function(){
// do nothing
}
}
enable();

setInterval() will not run immediately [duplicate]

I found a solution to run interval in javascript immidiately, not waiting for a first "timeout"
setInterval(function hello() {
console.log('world');
return hello;
}(), 2500);
But problem is this solution isn't working is construction like this
(function () {
window.Banner = {
doMagic: function () {
setInterval(function magic() {
console.log('magic');
return magic;
}, 2500);
}
}
})();
Banner.doMagic();
I've tried to return Banner method, to name it and return and no success.
Point is what in real construction is much more complex, so i can't just rewrite it to "return setTimeout" solution, as offered widely, so it would be really perfect to find what am i doing wrong with this one.
http://jsfiddle.net/5jawxLnr/3/
Perhaps the most proper way to do it would be to take the whole callback outside of the setInterval call and put it in a separate variable:
(function () {
window.Banner = {
doMagic: function () {
var magic = function() {
console.log('magic');
};
setInterval(magic, 2500);
magic();
}
}
})();
Banner.doMagic();
The effect is the same as your first code, but this way your code is a little cleaner.
Your no longer self-executing the function in your 2nd code snippet. You need to change this to the following:
doMagic: function () {
setInterval(function magic() {
console.log('magic');
return magic;
}(), 2500);
}
I agree with others though, this isn't the cleanest way to do this and isn't very obvious to the next developer who comes along. I recommend storing the function in a variable, executing it immediately and then running it in the setInterval also:
doMagic: function () {
var magic = function magic() {
console.log('magic');
return magic;
}
magic();
setInterval(magic, 2500);
}
If you add the parenthesis to the below code part it does
doMagic: function () {
setInterval(function magic() {
console.log('magic');
return magic;
}(), 2500); // added them here
}
In your code there is no way to perform the required task, instead follow the below approach:
// Use function to perform the task.
function doTask () {
console.log("...");
}
// Perform task for the first time.
doTask();
// On interval do task.
setInterval(doTask, 2500);
Try this example
var hello = function() {
document.write('hello... ');
};
setInterval(hello, 1000);
hello();

Why does setInterval initiate when I'm only assigning it?

I'm assigning to a variable, a function that uses setInterval, but I don't want the function to run until I call it. However, the function is running from just the assignment statement.
sessionClock = setInterval(function() {
console.log("Hi")
}, 1000)
I have also tried like this:
sayHi = function() {
console.log("Hi");
}
var sayHiStarter = setInterval(sayHi, 1000);
Both of these initiate the function and will log "Hi" to the console.
Why is it running on assignment? And what can do I do fix this?
If you only want to bind a function to setInterval, but call it later, you can use bind:
var sessionClock = setInterval.bind(null, function() {
console.log("Hi")
}, 1000);
//... later
var myInterval = sessionClock(); // start the timer
// ... later if you need to clear it
clearInterval(myInterval);
In principle, bind returns a new function that calls your original function (in this case, setInterval) with predefined arguments. So when you call sessionClock, that returned function is called. There a other aspects to bind, but they don't seem to apply in this context.
The call to setInterval does not return a function, but an identification for the created interval. This id is used to remove the interval when you don't want it to execute anymore:
sessionClock = setInterval(function() {
console.log("Hi")
}, 1000)
...
clearInterval(sessionclock);
What you want is something like this:
sessionClock = function () {
return setInterval(function() {
console.log("Hi")
},
1000);
}
//When needed
var intervalId=sessionClock();

Make a function that can perform multiple callback with one parameter

I'm working on a big project and I simplified what it matters here. This is the code:
a = new Thing(/*sayHi + sayHey*/);
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
I'd like to, with just the callback parameter, call both the sayHi() and the sayHey() function, at the order I put them. Is it possible? How would I do it? Thank you.
Pass an anonymous function that calls both of them sequentially:
a = new Thing(function() {
sayHi();
sayHey();
});
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
Alternatively to #Barnar's answer, create and pass a regular named function. If the callback logic gets heavier, you might want that anyway.
function hiHeyCallback() {
sayHi();
sayHey();
}
a = new Thing(hiHeyCallback);

jquery delay between javascript functions

I have multiply functions with parameters simplified as:
function f1(p1,p2){
alert('Function one is P1:'+p1+' P2:'+p2);
}
function f2(p1,p2){
alert('Function two is P1:'+p1+' P2:'+p2);
}
I need to fire these is a sequence with a delay between. I have however found that jQuery dislikes running functions with parameters. I have tried the .click function.
$.delay(1000).click(f1('One',false)).delay(1000).click(f2('One',false));
But the delay makes the click functions not work...
I would just use a simple timeout:
f1("one", false);
setTimeout(function() { f2("one", false); }, 1000);
$.delay(1000).click(function(){f1('One',false);}).delay(1000).click(function(){f2('One',false);});
not sure what the click is for though ...
if you want to delay a function call then a much simpler way is to use setTimeout().
eg:
// calling it in a nested setTimeout for sequential delayed execution
setTimeout(function(){
f1('One',false);
setTimeout(function(){
f1('One',false)
},300)
},300)
function fn1()
{
alert(1);
}
function fn2()
{
alert(2);
}
var arr=[fn1,fn2];
var len=arr.length;
var time=1000;
for(var k=0;k<len;k++)
{
(function(k)
{
setTimeout(arr[k],time);
}(k))
time=time*2;
}
It executes after a delay of 1 second!
DEMO

Categories