Determine order functions are called in [closed] - javascript

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

Related

Complicated repeated 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 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.

How can I extend jquery-comments? [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
I am using jquery-comments to allow for commenting on my website. This works fine, but I would like to make some changes to how it works.
However, instead of making my changes to jquery-comments directly in the jquery-comments.js, I would like to put them in a different file, and extend/modify the Comments object if possible with my own functions.
For example I would like to change what happens in the function called createCommentingFieldElement.
How can I do that?
This is how I solved it:
When I initialize jquery-comments I can extend it with a function like this:
$('#comments-container').comments({
getComments: function (success: any, error: any) {
var extensionMethods = {
doSomething: function () {
// this is my extended function.
// here you have the this object available.
}
}
};
$.extend(true, $('.jquery-comments').data('comments'), extensionMethods);
}
Your new jquery-comments extended function can now be called like this:
$('.jquery-comments').data('comments').doSomething();

New to testing, how would I test this method with Mocha, Chai, Enzyme, and Sinon? [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 6 years ago.
Improve this question
Here's my method
handleKeyEvent(event) {
const code = event.keyCode;
if (UsedKeys.includes(code)) {
event.preventDefault();
if (code === KeyCodes.DOWN) {
this.modifyIndexBy(1);
} else if (code === KeyCodes.UP) {
this.modifyIndexBy(-1);
}
}
}
I'm still pretty new to testing, and I have no idea how I'd go about testing this piece.
The method takes an event, so do I have to synthesize an event object and pass it in?
After that, do I just somehow test that this.modifyIndexBy() gets called?
This method doesn't return anything. Do I modify my code to be more testable?
There are a few ways you can do this.
You could create the object, and then spy on modifyRowIndexBy to check that it is called when handleKeyDown is called with the given parameter.
You could separate your key handling from your "index modifying" so that they can be tested independently (it is bad practice to spy on the same object that you are testing)
What does modifyIndexBy actually do? You could test that the outcome is the same (so it doesn't actually test that modifyIndexBy is called, but that what modifyIndexBy should do is done). This is a more BDD approach.

Get a list of games and their properties from JSON [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 7 years ago.
Improve this question
I'm trying to access a list of Casino Games and their properties from a JSON object in order to get them to display on the website. This is where I am so far.
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
console.log(x);
function ProcessProgressiveGames(progressiveGames) {
console.log(progressiveGames.d.Games[0].GameName);
}
What's there best way to do this. If you check your console, you'll see that var x contains the object with the game data.
See also: http://pasteboard.co/kXb1Voq.png
Related fiddle: http://jsfiddle.net/emporio/vz24dtm8/5/
This question is unique because it requires accessing unique properties. The answers also provide a multitude of ways to retrieve the data.
DEMO
JS
var Games;
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", function (data) {
Games = data.d;
}).done(function () {
document.getElementById('choice').innerHTML=ProcessProgressiveGames(Games);
});
function ProcessProgressiveGames(progressiveGames) {
return progressiveGames.Games[0].GameName;
}
$.getJSON function returns promise and the proper way to handle data returned from this address is to set second argument - function to handle data
$.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
function ProcessProgressiveGames(progressiveGames) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
console.log(progressiveGames);
document.getElementById('choice').innerHTML = ProcessProgressiveGames(x);
return progressiveGames.d.Games[0].GameName;
}
http://jsfiddle.net/vz24dtm8/7/
You can try like this
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
http://jsfiddle.net/vz24dtm8/8/
function ProcessProgressiveGames(data) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
return document.getElementById('choice').innerHTML=data.d.Games[0].GameName;
}
<pre>Use callback function to get the json value and pass the parameter in callback function</pre>
http://jsfiddle.net/rajen_ranjith/vz24dtm8/13/

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