Complicated repeated function [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 4 years ago.
Improve this question
Please Help me to solve this complicated loop. I’m calling 5 API URL but each of them should be called on a specific number of times then it should start to second, third, fourth and fifth URL and start again from top to bottom again and gain.
https://www.example1.com should be called 4 times
https://www.example2.com should be called 10 times
https://www.example3.com should be called 8 times
https://www.example4.com should be called 9 times
https://www.example5.com should be called 6 times
Should end on https://www.example5.com and again start from top https://www.example1.com
Unstoppable loop.
I highly Thanks & appreciate that anyone which answers this.
My code:
This is what I have tried so fo
The result of the code is commented inside the above code.

Use a variable as counter for each function like below,
var numberOfExecution=0;
function1(); // Start the procedure
function1()
{
// do api call
.......
// after finishing your task, check if this function execution hits desired number
numberOfExecution++;
if(numberOfExecution==4)
{
numberOfExecution=0;
function2();
}
else
{
function1();
}
}
function2()
{
// do api call
.......
// after finishing your task, check if this function execution hits desired number
numberOfExecution++;
if(numberOfExecution==6)
{
numberOfExecution=0;
function3();
}
else
{
function2();
}
}
In these process, one after another execution will continue achieving desired number of execution.

Related

how to write a complex form of setTimeOut and setTimeInterval with and without recursion? [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 4 months ago.
Improve this question
function sayHello() {
alert('Hello');
}
setTimeout(sayHello, 1000);
This is a basic setTimeout example, but can I get an in-depth explanation like a long one how it works with recursion and without recursion in javascript? (visualization helps as snippet code too)
You can call setTimeout() again inside the callback. This will print Hello in 1 second, then print World 2 seconds after that.
function sayHello() {
console.log('Hello');
setTimeout(sayWorld, 2000);
}
function sayWorld() {
console.log('World');
}
setTimeout(sayHello, 1000);

I have a function, that's invoked 12 times with different arguments, is it okay to have in code 12 function calls in a row? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a function, that's invoked 12 times with different arguments, is it okay to have in code 12 function calls in a row? That function draws cards from deck to players hands in my card game.
drawCard(userHand);
drawCard(userHand);
drawCard(bot1Hand);
drawCard(bot1Hand);
drawCard(bot2Hand);
drawCard(bot2Hand);
drawCard(bot3Hand);
drawCard(bot3Hand);
drawCard(bot4Hand);
drawCard(bot4Hand);
drawCard(bot5Hand);
drawCard(bot5Hand);
drawCard(dealerHand);
drawCard(dealerHand);
drawCard(dealerHand);
Yes, there is nothing inherently wrong with this.
Option 1:
If you would like to make your code a little more readable, you could add all the hands into a list and loop through the list evoking the function on each element.
let hands = [userHand, bot1Hand, bot2Hand, ...];
for (const hand of hands) {
drawCard(hand)
}
Option 2:
I see that you are calling the function twice for each. So, you could run the code in the drawCard() function twice using a loop in order to half the amount of function calls.
Option 3:
You could add another parameter to drawCard() which has the number of cards you would like to draw then return a list of the drawn cards.
You could try something like:
function test(a){
console.log(a);
}
my_list = [1,2,3,4,6,7]
for (const num of my_list) { test(num); }
In your example, my_list would be:
my_list = [userHand, userHand, bot1Hand, bot1Hand ...]

How can I solve this problem with the screen touches? [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 3 years ago.
Improve this question
I would like you to tell me if there is an event that returns to me when it was the last time a user touched the screen, since I need a function to run 3 seconds after the user touched the screen for the last time
There's no specific event for your use-case. What you can do however is adding a click event listener to your window object and assign the current time to a global variable as soon as someone clicked the screen. Additionally you have to use JavaScript's setInterval() method, which executes a function periodically e.g. every 20 ms. Inside it's callback function you can compare the current time to the time stored in the global variable and if the difference is bigger than 3000 trigger your action.
Here's an example (just click 'Run code snippet'):
var timeClicked;
function screenClicked() {
timeClicked = new Date();
}
function check() {
if (timeClicked != null) {
if (new Date() - timeClicked > 3000) {
clearInterval(interval);
alert("time's up!");
}
}
}
window.addEventListener("click", screenClicked);
var interval = setInterval(check, 20);
<body bgcolor="#eeeeee"><span>click anywhere inside this window</span></body>

Determine order functions are called in [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 9 years ago.
Improve this question
So I have a list of functions in JavaScript, each function has an associated button.
I want to know how you can tell in which order the buttons were pushed.
There's no native way for JavaScript to keep track of when a function was called, as this would have too much of a performance impact on the engine. You'll need to modify your code to keep track of this information internally. For example, you could use an array to log each call.
var log = []; // List of calls made in order
function one()
{
log.push('one'); // Log call
// ...
}
function two()
{
log.push('two'); // Log call
// ...
}
function three()
{
log.push('three'); // Log call
// ...
}
// Call in some order (which could be done by the user of course):
two();
one();
three();
You could of course easily reset your log as well:
log = []; // Reset

javascript return when stopping function [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 8 years ago.
Improve this question
If I write this:
$('#SomeDiv').click(DoSomething);
function DoSomething() {
if (SomeCondition === true) {
return false;
}
// more code here
}
regardless of whether I put return; or return false; or return true; the code doesn't throw exceptions and the function execution stops.
Which is the best option?
If those are the only options, use return; in this case. (read below)
If the function normally returns something (calculates something, gets some value, etc) then you definitely don't want to return anything, because you might confuse the caller.
If your function doesn't normally return anything, then it might not hurt to return anything you like, but it might still confuse callers.
I would personally rather just put an else after the if, and not use the return;. And if the function gets too large, just retractor it a bit.
If you just want to stop the function on some condition and don't care what it returns, then it doesn't matter which of the three you choose. If you're not using the output of the function, I'd just use a simple return; statement to stop it executing further.

Categories

Resources