repeat function with parameter by settimeout inside itselft [duplicate] - javascript

This question already has answers here:
Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()?
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I need to iterate a function with its argument but got the recursion error
Error in mounted hook: "InternalError: too much recursion"
the function:
methods: {
showSlides: function(n) {
console.log(n);
setTimeout(this.showSlides(n), 3000);
//setTimeout(this.showSlides(n++), 3000);
}
},

Related

How can I use the setTimeout() statement? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
How can I pass a parameter to a setTimeout() callback?
(29 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 3 months ago.
I was Coding Today (new) and i was wondering how people made js and python so i tried making a function based programming language using js
function init(){
log(console,"lmao ok")
setTimeout(log(console,'#--- Done'), 5000 )
}
function log(arg1,arg2) {
// body...
if (arg1 == console){
console.log(arg2)
}
else {
return
}
}
function prompt(arg1,arg2,arg3){
console.debug
var arg1 = "lmao"
}
init()
log(console,'tosay')

Does onclick = function put that function in a new scope? [duplicate]

This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Why use named function expressions?
(5 answers)
Closed 2 years ago.
Need your assistance. I have a function that was defined in onclick event and when i try to call it later there is an error that says function undefined.
author.onclick = function authorSlideInfo(event){
// do work
}
authorSlideInfo.then(() => {
// do work
}
Function returns a promise and i want to do something else after it ends. Is my function not in global scope? How to overcome this?

`this` is undefined when used in a Destructuring Parameter function [duplicate]

This question already has answers here:
Javascript OOP - lost this in asynchronous callback
(6 answers)
Javascript callbacks losing 'this'
(3 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 3 years ago.
I have this function inside my class which has Destructuring parameters
export default class MainCtrl {
constructor(mainService ) {
'ngInject';
this.mainService = mainService;
}
$onInit() {
navigator.geolocation.getCurrentPosition(this.search);
}
search({ coords }) {
console.log(this); // shows undefined
this.mainService.search(coords);
}
}
Now when im using this.mainService it says this is always undefined. How to pass the this instance on this kind of function?

What does this syntax mean in js? [duplicate]

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
const List = connect(mapStateToProps)(connectedList);
There is a function called "connect" being called with "mapStateToProps" as the argument, however there is "connectedList" surrounded by parenthesis right after the function calling. I haven't seen that before and I didn't find anything about that in the es6 articles I read for research.
The connect function most likely returns another function that accepts one argument which is being invoked.
function getFunc(arg){
alert(arg);
return function(arg2){
alert(arg2);
}
}
getFunc("arg1")("arg2");//invokes the getFunc function and invokes the returned function

Why can't window.location.reload be passed as a first class function? [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 5 years ago.
Why is it that this works,
setTimeout(function() { window.location.reload() }, 3000);
but this doesn't?
setTimeout(window.location.reload, 3000);
I receive the following error: TypeError: 'reload' called on an object that does not implement interface Location.
In theory it can be. When you pass it like that, it is just the function, without its execution context (this). Since the function (internally) makes use of this, it fails. You may notice this with console.log as well.
The solution is to bind the context:
setTimeout(window.location.reload.bind(window.location), 3000);

Categories

Resources