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);
Related
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
This question already has answers here:
Why does the setInterval callback execute only once?
(2 answers)
Closed 4 years ago.
i need to show current time in html code, but the javascript code only work once?
$(document).ready(function () {
function updateClock(ele){
var current_time = new Date();
var current_time_str = current_time.toLocaleTimeString();
ele.text(current_time_str);
console.log(current_time_str);
}
setInterval(updateClock($('#clock')) , 1000 );
})
It's work different some others languages like C,Object-C or Python, it's so misleading for me.
You need to wrap the calling part in a function, because it is called only once and returns undefined as calling value.
setInterval(() => updateClock($('#clock')), 1000);
Another possibillity is to use the third and following arguments of setInterval
setInterval(updateClock, 1000, $('#clock'));
Put the updateClock inside setInterval callback function
$(document).ready(function() {
function updateClock(ele) {
var current_time = new Date();
var current_time_str = current_time.toLocaleTimeString();
ele.text(current_time_str);
}
setInterval(function() {
updateClock($('#clock'))
}, 1000);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="clock"></div>
This question already has answers here:
Why does the setInterval callback execute only once?
(2 answers)
Closed 6 years ago.
function updateCounter(){
console.log(delay);
delay = delay - 1000;
$('#counter-value').html(delay / 1000);
if(delay <= 0){
clearInterval(loopID);
}
}
var delay = 5000;
var loopID = setInterval(updateCounter(), 1000);
I don't understand why it doesn't work, could someone help me? I've looked many things but couldn't end up making it. :(
You need to pass the function name or reference--remove ()
var loopID = setInterval(updateCounter, 1000);
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);
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;
});
}