NodeJS: Settimeout in prototypes [duplicate] - javascript

This question already has answers here:
setTimeout scope issue
(5 answers)
Closed 8 years ago.
I recently started using NodeJS, but when I do
I'm getting errors such as:
What's the best way to do this?
My current solution is like this:
var that = this;
setTimeout(function()
{
that.myMethod();
}, 3000);

you can use the old that=this trick, or use bind, since it's sure to work in node.js:
setTimeout(this.myOtherMethod.bind(this), 10);

Related

i cant pass a parameter to a function using javascript [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Closed 2 years ago.
i am trying to add a set time out function like this and it does not work
setTimeout(myfunction(parameter),200)
meanwhile the code below work but it does not get the job done cause i cant pass a parameter.can anyone explain why ?
setTimeout(myfunction,200)
Try
setTimeout(function() {
myfunction(parameter);
}, 200)
You can also use
setTimeout(myfunction.bind(null, parameter), 200);

setInterval functionality in javascript using functions [duplicate]

This question already has answers here:
Pure Javascript - setInterval (1s), setAttribute
(2 answers)
Closed 5 years ago.
how can i implement the javascript code for setInterval with using only javascript functions(i should not be using setInterval(function(){},1000)). and i should not be using any kind of libraries for making that possible.
Can any one please help me with this???
You can use recursive setTimeout
var timerId = setTimeout(function run() {
alert( "sec" );
timerId = setTimeout(run, 1000);
}, 1000);

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);
:)

setInterval and setTimeout don't seem to work with a console.log [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am answering my own question in the hopes it will solve someone else's headache:
I could not get the following code to work:
function givePosition(){
var place = $('#two').position();
console.log(place);
}
window.setInterval(givePosition(), 50);
The solution to my problem was to give the function name as a parameter without parentheses, like so:
window.setInterval(functionName, 50);
instead of:
window.setInterval(functionName(), 50)

what does (function () { ... })(); mean? [duplicate]

This question already has answers here:
What does this "(function(){});", a function inside brackets, mean in javascript? [duplicate]
(5 answers)
Closed 9 years ago.
I see that in many modules for node.js and also for browser, they use to have all their code inside something like this:
(function () {
moduleName.prototype.variable = 'whatever';
})();
Can anyone please explain what this all is, or any links that explain? I have no idea how to search it! Thanks in advance.
This is a pattern called Immediately Invoked Function Expression.
It's mostly useful for isolation purposes.

Categories