"Real" Sleep() Function without callback [duplicate] - javascript

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 6 years ago.
I know the time.sleep functiom from python. I just want to waot 3 seconds without making a callback. If i had to implemebt a callback i also had to hanfle global vars and more callbacks. I would like to have a function:
function getdata() {
console.log("hello");
Sleep(3000);
console.log("stackoverflow!");
return "my data";}
Please don't give me a solution for my specific example... i would also need sleep in while functions and other things. I searched a lot but i only found (in exanple setTimeout) functions with callback.

It doesn't exist. Javascript isn't designed this way. You need to use a callback and setTimeout. Sorry!

Related

Repeating a script using setInterval() [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I'm working on a Chrome extension to fetch tweets and I figured that I could use the setInterval() function to make the script run every minute. First I tried giving it the function like this:
setInterval(myFunction(), interval);
But it would only execute my script once.
Then out of curiosity I tried declaring the function in the setInterval() function like so:
setInterval(function() {body of my function}, interval);
And that works, but is not a very pretty solution, does anybody any other way of doing this or am I just going to have to deal with it?
Just remove the brackets from the first call. The reason for this is that you need to pass in the function, not the result of the function (what it returns).
When you write the function's name with brackets, it calls the function. When you exclude the brackets, it simply refers to the function like a variable, and so you can pass in your function to the setInterval() function.
setInterval(myFunction, interval);

how to run sequence functions in javascript [duplicate]

This question already has an answer here:
How can I write an async method in JavaScript when posting or looping? [closed]
(1 answer)
Closed 9 years ago.
How to run sequence functions in JavaScript:
I've 4 functions like this:
function myfunction()
{
callfunction1();
callfunction2();
}
In callfunction1:
function callfunction1()
{
callfunction3();
}
My problem is in "myfunction" when I call "callfunction1",callfunction1 is run and it call "callfunction3" but when "callfunction3" haven't finish,"callfunction2" is running.Have anyway to wait "callfunction3" finish then "callfunction2" is run ?
JavaScript is single-threaded, callfunction2 will only run after callfunction3 has compeletely finished.
However, if callfunction3 contains some AJAX or other asynchronous operation, then that will always happen after callfunction2 (because of the single-threaded thing, it can't start the asynchronous callback until after the synchronous stuff is done). Anything that relies on the result of an asynchronous function MUST either be in the function itself, or called by said function.

func_two does not trigger in javascript [duplicate]

This question already has answers here:
Calling a function in JavaScript without parentheses
(7 answers)
Closed 9 years ago.
I have javascript script code here. When I execute my code as it is then it does not trigger func_two function. But when I change following code in func_one
if (this.remove) {
this.func_two;
}
to this
if (this.remove) {
this.func_two();
}
Then it does trigger second function. But I want to trigger it this way this.func_one. IS it possible to do it this way? How?
You have to put () when you call a function, you can't just, out of nowhere, decide that you want it to work another way.
Take a look at this answer, it may help you.
this.func_two;
This statement return the function. It does not call the function. To call the function you have to add () at the end. or you have to do it like:
f2=this.func_two;
f2();

(function(){})() Is there any documentation of this? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?
I use in my scripts:
(function(){
...[code]..
})()
I can't find documentation about this, i have see scripts when this form take args.
(function(){
...[code]..
})(arg1,arg2)
Somebody have a link or a good explanation about this javascript function?
this is just regular javascript.
you instanciate an anonymous function, and then call it with 2 arguments.
The confusing part I think is the on-the-fly aspect of the operation.
You could have done (at higher cost):
var hnd = function() {...};
hnd(arg1,arg2);
It's known as a Self-Executing Anonymous Function.
Here is the first Google result, which gives a solid overview.

Is there a Sleep/Pause/Wait function in JavaScript? [duplicate]

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 3 years ago.
Is there a JavaScript function that simulates the operation of the sleep function in PHP — a function that pauses code execution for x milliseconds, and then resumes where it left off?
I found some things here on Stack Overflow, but nothing useful.
You need to re-factor the code into pieces. This doesn't stop execution, it just puts a delay in between the parts.
function partA() {
...
window.setTimeout(partB,1000);
}
function partB() {
...
}
You can't (and shouldn't) block processing with a sleep function. However, you can use setTimeout to kick off a function after a delay:
setTimeout(function(){alert("hi")}, 1000);
Depending on your needs, setInterval might be useful, too.
setTimeout() function it's use to delay a process in JavaScript.
w3schools has an easy tutorial about this function.

Categories

Resources