javascript setInterval only work once? [duplicate] - javascript

This question already has answers here:
Why does the setInterval callback execute only once?
(2 answers)
Closed 4 years ago.
i need to show current time in html code, but the javascript code only work once?
$(document).ready(function () {
function updateClock(ele){
var current_time = new Date();
var current_time_str = current_time.toLocaleTimeString();
ele.text(current_time_str);
console.log(current_time_str);
}
setInterval(updateClock($('#clock')) , 1000 );
})
It's work different some others languages like C,Object-C or Python, it's so misleading for me.

You need to wrap the calling part in a function, because it is called only once and returns undefined as calling value.
setInterval(() => updateClock($('#clock')), 1000);
Another possibillity is to use the third and following arguments of setInterval
setInterval(updateClock, 1000, $('#clock'));

Put the updateClock inside setInterval callback function
$(document).ready(function() {
function updateClock(ele) {
var current_time = new Date();
var current_time_str = current_time.toLocaleTimeString();
ele.text(current_time_str);
}
setInterval(function() {
updateClock($('#clock'))
}, 1000);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="clock"></div>

Related

How to stop this javascript count down when finished? [duplicate]

This question already has answers here:
Stop setInterval call in JavaScript
(7 answers)
Closed 3 years ago.
I found this javascript and it starts a new count down after finished in a loop
<script>
var countdownNumberEl = document.getElementById('countdown-number');
var countdown = 10;
countdownNumberEl.textContent = countdown;
setInterval(function() {
countdown = --countdown <= 0 ? 10 : countdown;
countdownNumberEl.textContent = countdown;
}, 1000);
</script>
SetInterval returns a unique ID.
var intervalId = setInterval( function(){}, 1000);
so, when you want it to stop, you will just call clearInterval(intervalId)
If you want to call it in the function itself, you need to just have the correct conditional to monitor when you want it to stop it you would need to reference the global or scoped identifier.
In your example, you are using a countdown variable.
So you can say something like:
if (countdown <= 0) clearInterval(intervalId);

JavaScript Call function then call it after x seconds continuously [duplicate]

This question already has answers here:
Execute the setInterval function without delay the first time
(18 answers)
Closed 5 years ago.
Following calls a functions every 10 seconds.
It does not call the function immediately, just after the first 10 seconds.
window.setInterval(function(){
/// foo
}, 10000);
How do I call the function first, then call it every x seconds, what would be the best way of doing this?
Either give it a name and call it right after the setInterval
function repeat(){
//foo
}
window.setInterval(repeat, 10000);
repeat();
Or use setTimeout instead and call it from inside the function
function repeat(){
//foo
setTimeout(repeat, 10000);
}
repeat();
EDIT:
<html>
<head><title>Timeout testing</title></head>
<body onload="callMe()">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
function callMe()
{
window.console.log('called');
window.setTimeout(callMe, 1000);
}
$(".realContent").click(function() {
var data = [ {"id":1,"start":"/Date(1401993000000+0530)/"} ];
var myDate = new Date(data[0].start.match(/\d+/)[0] * 1);
myDate = new Date(myDate.getTime() + myDate.getTimezoneOffset() * 60 * 1000);
alert(myDate);
})
</script>
</body>
</html>

setInterval function runs only once [duplicate]

This question already has answers here:
Why does the setInterval callback execute only once?
(2 answers)
Closed 6 years ago.
function updateCounter(){
console.log(delay);
delay = delay - 1000;
$('#counter-value').html(delay / 1000);
if(delay <= 0){
clearInterval(loopID);
}
}
var delay = 5000;
var loopID = setInterval(updateCounter(), 1000);
I don't understand why it doesn't work, could someone help me? I've looked many things but couldn't end up making it. :(
You need to pass the function name or reference--remove ()
var loopID = setInterval(updateCounter, 1000);

Send interval id as an argument to executing function [duplicate]

This question already has answers here:
In JavaScript, how can I access the id of setTimeout/setInterval call from inside its event function? [closed]
(2 answers)
Closed 6 years ago.
Is it possible to send interval id to executing function as an argument ? For example some thing like this:
var id= setInterval(myFunc.bind(null,id),1000);
What I am going to do is that in myFunc I want to do some processing and clear interval if I need. I can't use global variable because it will be happened multiple time. And I know that I can use global array but it would a little a bit of time consuming because of my function logic. So I want to know if I can pass interval id as an argument to myFunc.
EDIT:
this stackOverfllow link dosen't help me because there were no helpful answer.
I use this pattern:
function myFunc(host){
console.log(host.id);
}
var host = {};
host.id = setInterval(myFunc.bind(null,host),1000);
You could actually try mixing up setTimeout and setInterval to accomplish what you are looking for.
you could read more about setInterval on how to pass additional parameters to the functions.
var interval = 0;
function logId(param) {
interval++;
if (interval === 3) {
clearInterval(param);
}
console.log(param);
}
var id = setInterval(function() {
setTimeout(logId, 0, id)
}, 1000);
You can create your own function to store your callback function and interval id in the same object and pass that object to your interval function which will call the provided function with the interval id.
function setMyInterval(f,t) {
var handleMyInterval = function(ob) {
ob.func(ob.id);
};
var idOb = {func: f};
idOb.id = setInterval(handleMyInterval,t,idOb);
return idOb.id
}
You can use function inside you interval so you can do like this.
Create a function that will do interval and clear itself after a time you set.
intervalTime is time for loop interval and timeout is the time to clear interval loop.
both receive in millisecond.
function test(intervalTime,Timeout,log){
var id = setInterval(function(){
console.log(log)
},intervalTime);
setTimeout(function(){
clearInterval(id);
}, Timeout);
}
test(1000,10000,'test1');
test(1000,1000,'test2');

Is there any way to delay javascript execute like this? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to wait for a period of time after a function run
here is a sample of the code:
run1();
// delay 1 sec
run2();
// delay 1 sec
run3();
No, it is asynchronous things in browser, which allows to prevent delays in working with it by users. You may only use timeouts and callbacks.
For your proposes you may organize something like a queue:
var cur = 0;
var functions = [run1, run2, run3, ...];
var next = function () {
functions[cur]();
cur += 1;
if (cur == functions.length) clearInterval(interval);
};
var interval = setIntervar(next, 1000);
Use the setTimeout() function which allows you to delay x number of milliseconds before executing code.
e.g.
run1();
setTimeout(function() {
run2();
setTimeout(run3, 1000);
}, 1000);

Categories

Resources