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 3 years ago.
Improve this question
Please consider the following example.
var secondsCount = 0;
if( secondsCount <= 1800 )
{
setInterval(function(){
secondsCount++;
console.log( secondsCount )
}, 1000);
}
If we run the above code nearly to 1800 seconds ( 30 mins ) we could see that secondsCount value & actual seconds ( or minutes ) lapsed are not equal.
Try this...
var secondsCount = 0;
var x = setInterval(function() {
secondsCount++;
console.log(secondsCount);
if (secondsCount >= 1800) {
clearInterval(x);
}
}, 1000);
Note that, setInterval and setTimeout are forced to use at least the minimum delay. See Reasons for delays longer than specified. In practice, there's no guarantee that your callback will be called in the "exact" set amount of time.
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 1 year ago.
Improve this question
I have a problem I want the script not to run unless I am less than 5 trainqueue_barracks
but I put 100 here and it doesn't work either. What's wrong?
if ( document.document.getElementById("trainqueue_barracks") == null
|| document.document.getElementById("trainqueue_barracks").rows.length < 5
) {
As you have tagged jquery, you can simply do it like this:
if(!$('#trainqeueu_barracks').length || $('#trainqeueu_barracks tr').length < 5)
{
...
}
Note that !$('#trainqeueu_barracks').length will be always true in your case, so the second part won't actually matter. I think what you really need is:
if($('#trainqeueu_barracks tr').length < 5)
{
...
}
First off, you shouldn't need the first condition of your loop
document.document.getElementById("trainqeueu_barracks") == null
Also if you put 100 in their then it shouldn't work and your code is right. Unless you want it to run when rows.length is >= 5.
document.document.getElementById("trainqeueu_barracks").row.length >= 5
Is the opposite end of what you have.
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My code is not working so I went to the Chrome console to try and fix it and now that is just returning undefined when I try and run my while loop.
This is the code I entered into the console:
let total = 0;
let playerTotal = 0;
while(total >= 10){
if (total >= 10){
if (total > playerTotal){
console.log('total wins')
} else if(total == playerTotal){
console.log('tie')
} else {
console.log('player wins')
}
}
total += 1
}
Your condition is while (total >= 10). total starts out at 0. 0 is not >= 10 and so your loop's body is never executed.
You're seeing undefined because when you enter code in the console, the final result of that code is displayed. The result of a while loop is the value of the last statement evaluated within it (weird but true); a loop that never executes its body therefore results in undefined..
while(total >= 10){
should be changed to
while(total <= 10){
Thats logic issue which trigger infinite loop because total number would not increment since total is 0 and condition of loop only start when total >= 10.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a timer ( 15 ) and it count down till it reach 0 and then it repeat
their is a button that I want to click when the timer is less than 2
my code is :
setInterval(function(){
var x = document.getElementById('timer').innerHTML ;
function getSecondPart(str) {
return str.split('00<span>:</span>00<span>:</span>')[1];
}
if (getSecondPart(x) < 04) {
document.getElementById('bidButton').click();
}
} ,100);
but is return this error :
SyntaxError: expected expression, got ','
why ?
You're missing a bracket to close the function:
setInterval(function(){
if (getSecondPart(x) < 2) {
document.getElementById('bidButton').click();
}
} ,100);
Try to use more indentation and it will be clearer.
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 7 years ago.
Improve this question
Have a broad algorithmic question regarding the Flow of Logic using setTimeout() and Events .onclick() in JavaScript.
Basic Procedure:
When a button is clicked 2 times within 3 seconds, a HTML element
(currently visible) dissapears, following this, when the same button
is pressed the image reappears and the process repeats
What would be the best way to come about this problem? Outline of code is appreciated.
Have been working on this for several hours now, my code written is logically incorrect and would not be much good use.
I hope this could help:
http://jsfiddle.net/kqzdn8xe/
$(document).ready(function(){
$('#button1').click(function(){
if (typeof(this.visibleFlag) == 'undefined') {
this.visibleFlag = true;
}
var thisTimeClick = Date.now();
if (this.prevClick && (thisTimeClick - this.prevClick < 3000) && this.visibleFlag) {
this.visibleFlag = false;
$('#div1').hide();
} else if (!this.visibleFlag) {
this.visibleFlag = true;
$('#div1').show();
}
this.prevClick = thisTimeClick;
});
});
I believe you are after something like this;
I have also included logic to ignore the case of a 3rd successive click (within 500ms of the 2nd one), as I assume you are after double click like behavior.
It would be worth also looking at the jQuery double click event: https://api.jquery.com/dblclick/
<button id="buttonExample">Click me</button>
<br/>
<div id="imageContainer">Image</div>
<script type="text/javascript">
$('#buttonExample').click(function(){
var timeNow = new Date().getTime();
var lastClicked = parseInt($('#buttonExample').data("lastClicked")||0);
var ms = timeNow - lastClicked;
if($("#imageContainer").is(":visible")) {
if(ms < 3000) {
$("#imageContainer").hide();
}
$('#buttonExample').data("lastClicked", timeNow);
}else if(ms > 500){
$("#imageContainer").show();
}
});
</script>