How do I set a delay with setTimeout()? [duplicate] - javascript

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 9 months ago.
I have code that is supposed to make a one-second-delayed loop, but it instead executes the code in the loop constantly.
loop1()
function loop1() {
setTimeout(loop2(), 1000)
}
function loop2() {
console.log("hey")
setTimeout(myLoop(), 1000)
}

Related

Javascript how i call function inside another function [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Javascript call nested function
(11 answers)
Closed 2 months ago.
I have this function inside a function, how i call fullName function?
var person = function(){
function fullName(){}
}
person.fullName(); This doesn't work

How to call functions dynamically in Javascript [duplicate]

This question already has answers here:
How to pass extra parameter to event handling callback?
(2 answers)
Pass an extra argument to a callback function
(5 answers)
Closed 5 years ago.
I have a function where I would like to call classList methods dynamically, something like this:
function expand(action) {
searchContainer.classList[action]("expanded");
}
button.addEventListener("click", expand('add'), false);
searchContainer.addEventListener("onblur", expand('remove'), false);
But not sure how can I do that?

How can I set delay for running functions [duplicate]

This question already has answers here:
How to create javascript delay function [duplicate]
(3 answers)
Closed 6 years ago.
I have javascript for call 2 function for example:
system.library.myscript.function1();
system.library.myscript.function2();
I would like to set delay before run function2 for example 20 seconds.
Highly appreciated for anything helps.
Use setTimeout() function:
setTimeout(function(){
alert("Hello");
}, 3000);
:)

Synchronous execution of setimeout [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 11 months ago.
How would I convert the following:
while True:
# do something
time.sleep(2)
into JavaScript?
You would not, as JavaScript does not sleep - it is synchronous and event-based. Yet, you can schedule functions to be executed later in time via setTimeout and setInterval:
var timerid = setInterval(function() {
// do something
// instead of "break", you'd use "clearTimeout(timerid)"
}, 2000);
For your ajax progress bar, I'd recommend the following which does not fire requests strictly each 2s, but waits for them to return:
function getUpdate() {
myAjax(…, function onAjaxSuccess(result) { // an async event as well
// show(result)
if (!result.end)
setTimeout(getUpdate, 2000);
});
}
getUpdate();
setInterval(function() {
console.log('do something');
}, 2000);
http://jsfiddle.net/rS9bH/

Python while loop conversion to Javascript [duplicate]

This question already has answers here:
Sleep in JavaScript - delay between actions
(15 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 11 months ago.
How would I convert the following:
while True:
# do something
time.sleep(2)
into JavaScript?
You would not, as JavaScript does not sleep - it is synchronous and event-based. Yet, you can schedule functions to be executed later in time via setTimeout and setInterval:
var timerid = setInterval(function() {
// do something
// instead of "break", you'd use "clearTimeout(timerid)"
}, 2000);
For your ajax progress bar, I'd recommend the following which does not fire requests strictly each 2s, but waits for them to return:
function getUpdate() {
myAjax(…, function onAjaxSuccess(result) { // an async event as well
// show(result)
if (!result.end)
setTimeout(getUpdate, 2000);
});
}
getUpdate();
setInterval(function() {
console.log('do something');
}, 2000);
http://jsfiddle.net/rS9bH/

Categories