How to execute function on specific interval of time in angular [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to execute function in angular on a specific time interval and how can we stop that too. Please give me solution for that.

Your question seems to be about JavaScript in general, not specifically Angular.
You could use setInterval and clearInterval to execute a function on a fixed interval:
// Will print each second
let interval = setInterval(() => console.log("I am a text"), 1000)
// Stops the interval
clearInterval(interval)
Documentation: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

Related

How to auto run a code multiple times on a site [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am looking for a javascript code which will automatically run itself multiple times on a site when i enter it in console . I have already tried and it works only once. Is there any way to automatically run it again and again ?
You can use setInterval function.
Example:
function myFunction(){
console.log('Success!');
}
setInterval(function(){
myFunction();
}, 3000); //you can set the interval here.

Angular 7: trying to delay router.navigate with setTimeout not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.
Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.
signInSuccessHandler() {
...
const token = this.getToken();
this.setToken(access_token);
setTimeout(() => { this.delayNavigation(); }, 1000);
}
delayNavigation() {
this._zone.run(() => this.router.navigate(['/people']));
}
Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.
A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.

Enable HTML Button after certain delay [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I display a countdown in a button, when the countdown gets to 0, it enables the button. Is this even possible? Thanks.
You use setInterval to execute a function periodically (every 1'000 ms). In the function that you pass to setInterval, you decrement the counter. You check if you have reached 0, and if so, you enable the button (and clear the interval).
Use .setInterval() for the countdown.
Remove the HTML disabled Attribute at the end of the countdown.

How to refresh a javascript variable in relation of time without refreshing the page? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In an unknown time the variable it deals with get a new value. I would like to refresh the output at the page withoout reloading it every 10 seconds.
How to? Please help.
you can use something like setInterval, https://www.w3schools.com/jsref/met_win_setinterval.asp
setInterval(function() {
//do what you need to with the variable here
}, 10 * 1000); //since millseconds
Use setInterval.
setInterval(function() {
}, 10*1000)
as said.
BUT...
If the new value of the variable, has to do with time, you 'd better use a timestamp method too, instead of doing your calculations based on setInterval.

Javascript, how do I make it loop this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I wish to loop the javascript going to a certain page (without actually reloading/clearing console), then execute
href="javascript:doSomething(9)"
and then loop that by doing a function
Please help, thanks!
Assuming you want it to repeat - you can use an interval.
setInterval(function(){
doSomething(9);
},100); // the 100 is for 100 miliseconds
If you want it to repeat without having to wait (that is, block the code) you can use a normal while loop:
while(true){ // will loop forever since the condition is always "true"
doSomething(9);
}

Categories