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

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

Related

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

Javascript function timer. Not working as expected [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 5 years ago.
Problem: Hello i'm a little stuck as to why setTimeout() does not call the function specified after the time passed I've tried a few things but nothing seems to work
Solution: If anyone knows any other way to call a function after a specific time
Here is my code :
refreshStats: function(){
this.goldLabel.text = Math.floor(this.player.data.gold);
this.attackLabel.text = Math.floor(this.player.data.attack);
this.defenseLabel.text = Math.floor(this.player.data.defense);
if (this.player.questsDone.length > 0){
console.log(this.player.questsDone)
this.bpText.text = this.player.questsDone[this.player.questsDone.length-1];
setTimeout(this.FadeConsoleText(), 5000);
}
},
FadeConsoleText: function(){
console.log("log");
},
Current output:
"Quest"
"a"
wanted Solution output:
"Quest"
(wait then call function)
"a"
Thank you in advanced
You want to pass the function, not what the function returns to setTimeout:
setTimeout(this.FadeConsoleText, 5000);

How to pass parameter to callback in javascript [duplicate]

This question already has answers here:
JavaScript: Passing parameters to a callback function
(16 answers)
Closed 6 years ago.
I would like to pass some parameter in call back like the following example. Is it a possible? Or I need to do it in another way?
function testCallback(callback, destination){
// how can I pass the destination to callback
callLib(callback)
}
function myCallback(destination){
//do something
}
callLib is an external library that accept. I guess the interface should be like this
function callLib(callback){
callback("something")
}
this one I think is not duplicate one. Sorry for my bad english. I have already change the question. see if you understand what the problem is
I played with callbacks all day to learn how to do this. Here is some example code: https://stackoverflow.com/questions/37554672/jqueryui-dialog-doesnt-stop-code-to-wait-for-user-input/37556573#37556573

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)

NodeJS: Settimeout in prototypes [duplicate]

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

Categories