Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have the following js function.
function showAttentionItem(sender)
{
debugger;
var assistanceRequired = sender.parent().hasClass("assistanceRequired");
if (assistanceRequired)
{
sender.children('.assistanceRequiredText').fadeToggle(0);
if (sender.children('.assistanceRequiredText').is(":visible"))
{
sender.children('.studentPerformanceText').hide();
}
}
if (!assistanceRequired)
{
if (sender.parent().hasClass("studentOutsideTargetRange"))
{
sender.children('.studentPerformanceText').fadeToggle(0);
}
}
}
What happens when I run it is, I hit the debugger line, and step through the code. assistanceRequired is true.
After executing sender.children('.studentPerformanceText').hide();, the next line it jumps to is sender.children('.studentPerformanceText').fadeToggle(0);, inside the if (!assistanceRequired) statement!
How could this possibly be happening?
This is guaranteed to work:
if (sender.parent().hasClass("assistanceRequired"))
{
// do whatever here
}
else
{
// do whatever ELSE here
}
Only one or the other can ever run when coded correctly since there is only one test and it will either be true or false the single time it is tested. There is no possibility for it to be changed by some side effect you are not aware of, which is most likely what is happening. This is not debatable.
If this does not work the way you want then something else is wrong that you are not showing and without an executable jsFiddle good luck convincing anyone that a fundamental thing like the if ... else construct is broken.
I can think of only one scenario to explain what you are seeing.
There's a double trigggering of showAttentionItem(), which, in the debugger, is perceived as a single call. On the first call, assistanceRequired is true and on the second it's false.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Does anyone have any idea how to stop an interval that is situated inside of a function once it's done doing its thing?
Here is what I mean:
function renderMessage(message) {
const renderInterval = setInterval(() => {
characterIndex++;
dealerMessage.innerHTML = `
${messages[message].slice(0, characterIndex)}
`;
if (characterIndex === messages[message].length) {
clearInterval(renderInterval)
}
}, 100);
}
As you can see, I'm trying to render out a message using this function. It does its job fine, but if I don't stop it, subsequent messages keep overriding themselves...
I've tested the if check and it is actually functioning inside the function, yet for some reason the clearInterval doesn't work.
Is there any way I can fix this, or do you recommend me to start from scratch?
Note: this method would be very handy for me, so, if possible, I would like to keep it.
I think your 'if' statement of clearInterval should be
if (characterIndex===message[message.length]){}
Also, I cannot see any initialization of the characterIndex variable. Please do inform if this worked or not.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm sure this is a very simple solution. I have made this javacript function that tests whether there is a certain css style on a div and then moves around another div. However, it does not work and I have no idea why.
JavaScript:
function sale() {
var style = document.getElementsByClassName("product-single__price--wrapper").getAttribute("style");
if (style !="display: none;") {
document.getElementByClassName("product-single__description").style.marginTop = "70px !important";
}
}
window.onload = sale;
I wouldn't ever suggest doing this, but if you want to call that function all the time, you need to put it into a setInterval with the milliseconds you want it to get called.
Example:
$(document).ready(function() {
setInterval(function() {
sale();
}, 1000);
});
OR
$(document).ready(function() {
setInterval(sale, 1000);
});
This will get called every second. Again, horrible horrible horrible practice. But, this will do what you want. If you want it called sooner, then change the milliseconds accordingly (1000 milliseconds = 1 second).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this code to add elements to my table and that part works perfectly fine. However, I want to add up the values of the last column in order to later print that on another part of the page. parseInt doesn't work for some reason (the values don't come back as integers and stay as strings, is there anything I have done wrong? Again, adding to the table works perfectly fine.
var hourC = 0;
var hourC =+ parseInt($(".hourCount").text(), 10);
alert(hourC);
Edit:
When I print the values of the variable hourC they don't add up to the previous value, they just stay next to each other. Example: 1 + 1 = 11 rather than 2. I don't see where my issue is and the answer for debugging didn't help since I still got the same result.
Final Edit:
I achieved what I wanted now through a different medium, I created an array and pushed the values into the array and then I used "join" to solve the issue.
If interested in what I was asking for here is a fiddle with the final result. (You can just change the console.log to alert)
Basic debugging skills.
parseInt works -- its a well tested function. If it broke in a browser, a lot of people would notice.
However, you haven't given any way to figure out what is going on with your code, since the real problem MUST be here:
$(".hourCount").text()
That must not contain whatever value you think it does.
Split your code up and use the debugger, or even console log, to see what the values are.
var hourC = 0;
var strValue = $(".hourCount").text();
alert(["strValue = ", strValue]);
hourC = parseInt(strValue, 10);
alert(hourC);
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 9 years ago.
Improve this question
I've recently started using Snippet 2 instead of Snippet 1, for basic selection statements. This seems a bit un-conventional, so I wanted to know if there is performance difference, or if one seems more readable than the other.
Snippet 1
function foo() {
if(case1){
//...
} else if (case2) {
//...
} else if (case3) {
//...
} else{
// catch other cases
}
}
Snippet 2
function foo() {
if(case1){
//...
return;
}
if(case2){
//...
return;
}
if(case3){
//...
return;
}
// catch other cases
}
Your snippets are the same thing. But I'd use elseif even if I return. It kind of gives me a sense that those if statements are related and helps me with readability.
if ... else also helps in performance, let's say, when you're if-ing the same variable. Why test a condition if the previous one succeeded? elseif it so it won't try again.
It's a matter of design. It depends on what you want to achieve so there's no EXACT BEST way of doing it. It's all a matter of how you need to exit your code block that contains the ifs.
The Ifs alone mean that all of the conditions can possibly happen - they'll be checked one by one. The chained if-else statements mean that only one of the conditions can actually happen - if one of the conditions get executed, I believe it skips over all of the rest. Most languages support a switch statement as well, which would be somewhat more readable than chaining together a bunch of if-else-if statements. Correct me if I'm wrong, but I believe this applies in every language I've used so far.
Edit: In this particular case (as pointed out by some others below - thank you), all of the conditions cannot happen because of the return statements in each of the if statements. In a more general case though, my explanation still holds.
It depends on your needs. As a general rule, the more exit points you have to a function, the harder it is to read. That said, there is often benefit of
if(somehting)
{
// one liner;
return;
}
if(something else)
{
// 30 lines of something else.
return;
}
// catchall
instead of:
if(something)
{
// one liner;
return;
}
else if(something else)
{
// now nested! 30 lines.
return;
}
Chances are, though, that you could probably make things even easier to read by turning those conditions into their own functions. Wouldn't this be nice:
if(something)
{
doSomething();
}
else if(something else)
{
doSomethingElse();
}
else
{
doJustElse();
}
The benefit of the last one? Succinct, clear, clean, obvious, and a low learning curve.
If you have too many else if statements then it always better to use switch rather than using if.
Snippet 1 is going to be better for performance when you don't need case 3 to be satisfied, since the other else if will not be fired. However, if you have a lot of other else if, you should use a switch
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.