How to pass parameter in function in modular pattern in javascript [duplicate] - javascript

This question already has answers here:
Why is JavaScript's Set Timeout not working? [closed]
(7 answers)
Closed 7 years ago.
Here is my Javascript function;
var searchbox=function(){
var _expandbox=function(count){
};
var _events=function(){
setTimeout(_expandbox,3000);
}
var _init=function(){
_events();
};
return {
init: _init
};
}();
$(document).ready(function(){
searchbox.init();
});
Here the problem is if I call function like setTimeout(_expandbox(4),3000) it won't work.So plese help me how to add parameter in function.

Try wrapping it in an anonymous function:
setTimeout(function() { _expandbox(4); }, 3000);

Related

How to use this inside a function which is inside a class method in Javascript [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Pass correct "this" context to setTimeout callback?
(6 answers)
Closed 1 year ago.
I'd like to use this to referring to the class properties while instead is referring to the function itself. How can I do?
class Person {
isWalking = false
steps = 10
walk() {
this.isWalking = setInterval(function () {
if (this.steps <= 1) {
clearInterval(this.isWalking)
this.isWalking = false
}
--this.steps
console.log(this.steps)
}, 1000)
}
}
Covert your setInterval callback to an arrow function

New to javascript and trying to use setTimeout [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Pass a JavaScript function as parameter
(15 answers)
Closed 2 years ago.
I'm quite new to javascript and I want a the crickit.motor1.run(60) to get set to 0 after 2 seconds but it isn't working and I'm kinda out of options for what to do next.
This is my code
forever(function() {
if (crickit.touch1.touchRead() > 400) {
light.setPixelColor(0, 0x00ffff)
crickit.motor1.run(60);
}
pause(100)
})
function motorOff() {
crickit.motor1.run(0);
}
forever(function() {
if (crickit.motor1.run() = 60)
setTimeout(motorOff() {
}, 2000);
})
These will work:
setTimeout( motorOff, 2000);
setTimeout( 'motorOff()', 2000);
setTimeout( function() { motorOff() }, 2000);
You have to use it like this:
setTimeout(motorOff, 2000);
If you have a function that uses parameters, lets say motorOff(param) would accept 1 paramter, you have to use it like this:
setTimeout(motorOff, 2000, param);

Why this code result is undefined? [duplicate]

This question already has answers here:
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Closed 5 years ago.
function aaa() {
return
{
test: 1
}
}
console.log(aaa());
Why this code result is undefined?
I assume is will be object.
Because when you change the line it terminate the statement so you have to put curly bracket after return to make it work.
Just use this:
function aaa() {
return {
test: 1
}
}
console.log(aaa());

javascript manipulating values within the scope chain [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I've been reading up on Javascript closures and scope chains, but I haven't seen anything about maniuplating variables from within the scope chain. Here's a similar type of scenario I'm running into:
function first() {
var a = [];
a.push({firstFunction: 'yes'});
doSomethingFunction(valueToPassIn, function() {
a.push({secondFunction: 'yes'});
doAnotherThingFunction(newValueToPassIn, function() {
a.push({thirdFunction: 'yes'});
})
})
console.log(a) //returns {firstFunction: 'yes'}
}
how can I get it to return {firstFunction: 'yes', secondFunction: 'yes', thirdFunction: 'yes'}
The code may have syntax errors, but it's the idea I'm trying to understand. I just wrote this code up on the fly so you guys could see a similar scenario as to what I'm trying to fix.
Thanks
I know this was answered in the comments but here is an example of using a callback.
function first(callback) {
var a = [];
a.push({firstFunction: 'yes'});
doSomethingFunction(valueToPassIn, function() {
a.push({secondFunction: 'yes'});
doAnotherThingFunction(newValueToPassIn, function() {
a.push({thirdFunction: 'yes'});
callback(a);
});
});
}
first(function(a){ console.log(a); });
The only problem with this method is that it gets unruly when you have more than 3 or 4 nested callback functions. Promises are the way to handle it.
jsfiddle: http://jsfiddle.net/axqmvdxg/

How would I have the 'this' keyword in a setTimeout parameter? [duplicate]

This question already has answers here:
Pass correct "this" context to setTimeout callback?
(6 answers)
Closed 9 years ago.
I have the following code with the purpose of making each .feedcontain div fade in after an increasing delay. The animation and formatting is perfect, its just that I can't have a this keyword in the first setTimeout() parameter.
function goFadeNow(){
var loopdelay=250;
$(".feedcontain").each(function() {
setTimeout('$('+this+').addClass("appeared");',loopdelay);
//$(this).addClass("appeared");
loopdelay=loopdelay+250;
});
}
If I uncomment line 5 and comment line 4, it works but it doesn't have the delay. PS: I do realize that I can't just use this like a normal variable.
You can also bind() the function you are passing to this pointer:
function timeoutFunc() {
$(this).addClass("appeared");
}
function goFadeNow(){
var loopdelay=250;
$(".feedcontain").each(function() {
setTimeout(timeoutFunc.bind(this), loopdelay);
loopdelay=loopdelay+250;
});
}
function goFadeNow(){
var loopdelay=250;
$(".feedcontain").each(function() {
var $this = $(this);
setTimeout(function () {
$this.addClass("appeared");
}, loopdelay);
loopdelay=loopdelay+250;
});
}

Categories

Resources