How can I set settimeout for a function, which will be start after 5 sec.
function test(fnn) {
setTimeout(fnn,5000);
}
test($('#id').addClass('qwerty'));
Passed parameter fnn is a function:
test(function(){
$('#id').addClass('qwerty');
});
You can use it.
setTimeout(function() { your_function(); }, 5000);
Related
I have some code which can be mimicked by this situation
function abc(def){
setTimeout(1500,function(){
console.log('1');
}
def();
}
function def(){
console.log('2'}
}
I want 2 to be printed after 1 - how do I do that?
If you want a function to also be called after a delay... just put it inside the delay. I've added some extra console logs and fixed some of your code. Run the below example to see how it works.
function def() {
console.log('2 - def()');
}
function abc() {
console.log('before');
setTimeout(function() {
console.log('1');
def();
}, 1500);
console.log('after');
}
abc();
function abc(def){
setTimeout(1500,function(){
console.log('1');
def();
}
}
You can use async and await. Wrap the setTimeout(...) in a Promise and resolve Promise inside the callback passed to setTimeout.
async function abc(def){
await new Promise(res => {
setTimeout(function(){
console.log('1');
res();
},1500)
})
def();
}
function def(){
console.log('2')
}
abc(def)
setTimeout(function, milliseconds) takes a function has the first parameter and delay value (time in milliseconds) as the second parameter. You were passing them in the wrong order.
Also in your example def() was printing 2 and it was outside the setTimeout function, so it could not have possibly printed 2 after 1 (as 1 was being logged inside the setTimeout, which means it would be executed after the delay of 1.5 seconds).
The solution provided by Chris above is correct and would do what you wanted (print 2 and then 1)
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);
So I basically got a variable that is set to true, and what I want to achieve is so the variable turns to false after 2 seconds. How can I do this with the use of setInterval ?
Try using setTimeout instead. It will execute the anonymous function once.
var yourVar = true;
setTimeout(function(){ yourVar = false; }, 2000);
If you are trying to trigger some event or call some functions after 2 seconds, then:
setTimeout(function() {
function1();
function2();
}, 2000);
// this will call functions after 2 seconds, when setTimeout will get called
And, if you want to call some functions after some particular event (condition) using setInterval then you have to use clearInterval to break the loop:
var setint = setInterval(function(){
if (check condition here) {
function1();
function2(); // .... so on
clearInterval(setint); // break the setInterval loop
}
}, 100);
I am trying to update a variable every one second. For that reason I am using setTimeout. But it does not update the variable. It logs out 0 just once. Here is my code
var yes=0;
setTimeout(function () {
console.log(yes);
yes++;
}, 1000);
Use setInterval, to finish the repitition you have to clear the interval using clearInterval(yourInterval);
Live Demo
var yes=0;
yourInterval = setInterval(function () {
console.log(yes);
yes++;
}, 1000);
setTimeout does repeat itself only once, try setInterval so that for every second it increments yes with 1
but remember to clear the interval after sometime else you get on to a never ending loop
using clearInterval()
setTimeOut method is just called once after a specific timeout which in your case is 1000, try to use setInterval method instead
jsBin demo with setTimeout
If you really want to stick to setTimeout (I appreciate that ;) )
than just wrap all into a function and recall it inside it self like:
var yes=0;
(function loop(){
setTimeout(function () {
console.log(yes);
yes++;
loop(); // loop recall
}, 1000);
})();
Otherwise go for setInterval:
var yes=0;
function count(){
console.log(yes);
yes++;
}
setInterval(count, 1000);
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