Javascript setInterval is not running [closed] - javascript

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 5 years ago.
Improve this question
the following code returns nothing. Is there something I am missing here? Shouldn't this say 'hi' twice...
Thanks in advance
var done = 1;
var id;
id = setInterval(function() {
if(done > 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);

The if statement in the interval, directly terminated the interval because 1 > 3 == false
var done = 1;
var id;
id = setInterval(function() {
if(done < 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);

Related

synchronize two javascript 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 5 years ago.
Improve this question
i am trying to synchronize two javascript function which uses the same variable.like say for example i is a variable and its initial value is zero first function increase this value by 1 and other one decrease the value by 1 but after the first function call only
Thanks in advance.
You can set timeout between two function.. May be what u want is this...
<html>
<script>
var i=0;
window.setTimeout(function() {
window.setTimeout(function() {
}, 10);
}, 30);
function loop() {
var args = arguments;
if (args.length <= 0)
return;
(function chain(i) {
if (i >= args.length || typeof args[i] !== 'function')
return;
window.setTimeout(function() {
args[i]();
chain(i + 1);
}, 2000);
})(0);
}
function abc(){
//console.log("kishan");
i++;
console.log(i);
}
function def(){
//console.log("oza");
i--;
console.log(i);
}
for(var u =0 ;u<5;u++){
loop(abc,def);
}
</script>
</html>

Add function to Object with an element selector [closed]

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 an Object
function Object(name, button) {
this.name = name;
this.button = button;
var alert = function () {
alert("x");
};
}
var ExampleOne = new Object('Example One', $('.exampleButton'));
How can I make the function inside the object fire on an event of Object[button]...
Using -
ExampleOne.button.click(function () {
ExampleOne.alert();
});
Doesn't work.
var alert = function is a local function and cannot be accessed outside that object. If you want it to be accessible with new instances, declare it with this:
function Object(name, button) {
this.name = name;
this.button = button;
this.alert = function () {
alert("x");
};
}
Fiddle: http://jsfiddle.net/qow3qu0m/2/
It's working for me. Check your code again. Maybe you forgot to include jQuery?
ExampleOne.button.click(function(e) {
alert('success!');
});
http://codepen.io/anon/pen/yyGpLz

How do I increment the value of a variable by 1 every 3 seconds? [closed]

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 8 years ago.
Improve this question
$(document).ready(function() {
var addTime = function() {
var time = 1;
setInterval((function() {
time = time + 1;
}), 3000);
console.log(time);
};
addTime();
});
Right now, it only outputs 2. What Am I doing wrong?
It is being incremented. But it's printed only once.
try this
$(document).ready(function() {
var addTime = function() {
var time = 1;
setInterval((function() {
time = time + 1;
console.log(time);
}), 3000);
};
addTime();
});
Below javascript function would help you.
window.setInterval("javascript function",milliseconds);
And here is the sample code
var myVar=setInterval(function(){myTimer()},1000);
function myTimer()
{
// code you want to execute
}
You can also refer the Jquery Timer..below is the link
http://jchavannes.com/jquery-timer/demo
Here are the multiple options available
var timer = $.timer(function() {
alert('This message was sent by a timer.');
});
timer.set({ time : 5000, autostart : true });
timer.set(options);
timer.play(reset); // Boolean. Defaults to false.
timer.pause();
timer.stop(); // Pause and resets
timer.toggle(reset); // Boolean. Defaults to false.
timer.once(time); // Number. Defaults to 0.
timer.isActive // Returns true if timer is running
timer.remaining // Remaining time when paused

Javascript function not called when used with IF statement [closed]

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 8 years ago.
Improve this question
My checkforZeroQuantity function is not getting called if I use it with if. Following is the sample code
function myButtonClicked() {
if (checkforZeroQuantity()) {
alert("checked");
}
}
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
retrun true;
}
}
This is because you have spelling mistake near as shown below
else {
retrun true; // it should be return
}
correct the spelling and try again.
look here:
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
return true;
}
}
There spelling mistake in code, please change 'retrun' to 'return'.
else
return true;
Firstly, there is a typo in the else block.:)
Secondly, it's a good practice to use === instead of == in Javascript

What does the false in addEventListener('click', stopEvent, false) mean? [closed]

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 9 years ago.
Improve this question
What does the third argument 'false' represent in the last line of the bellow code?
var parent = document.getElementById('parent'),
child = document.getElementById('child'),
op = document.getElementById('op'),
op2 = document.getElementById('op2');
parent.addEventListener('click', function () {
op.innerHTML += '<p>click registered</p>';
}, false);
function stopEvent (e) {
e.stopPropagation();
op2.innerHTML += '<p>propagation stopped</p>';
}
child.addEventListener('click', stopEvent, false);
if it is true it would be considered at the beginning of the all other added function (it is called captured), if not it would be just simply add to the end of list (called bubbled), let's say you have:
parent.addEventListener('click', function () { console.log("nocapture1"); }, false);
parent.addEventListener('click', function () { console.log("usecapture1"); }, true);
parent.addEventListener('click', function () { console.log("nocapture2"); }, false);
parent.addEventListener('click', function () { console.log("usecapture2"); }, false);
then once it is clicked, you would have them with this order in the console:
usecapture1
usecapture2
nocapture1
nocapture2
It specifies whether or not you want to capture the event. In this case, it isn't necessary because it defaults to false anyway. See the MDN documentation for element.addEventListener for more information.

Categories