I need this loop to run forever, a
while
for
for
while
display loop counter
wait 5 seconds
display curr time
wait 3 seconds
display time diff
(loop) display next loop counter
etc.
//$('body').css('background','wheat'); //test if js is broken
function waitx(truck){
console.log(truck);
$('div').html(truck);
setTimeout(function(){
newnow = new Date().getTime();
var diff = newnow - truck;
console.log(diff);
$('div').html(diff); //unwanted - just shows what SHOULD happen below
return diff;
},3000);
}
for (var nn=1; nn<6; nn++){
console.log(nn);
setTimeout(function(){
now = new Date().getTime(); //1479575397219
bonk = waitx(now);
//$('div').html(bonk + " hello"); //how to get this to work?
},5000);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>
Sounds like you're looking for a function that calls itself over and over again
(function waitx(i) {
console.log('loop counter : ' + i);
setTimeout(function() {
var now = new Date().getTime();
console.log('current time : ' + now);
setTimeout(function() {
var diff = new Date().getTime() - now;
console.log('time diff : ' + diff);
waitx(++i);
}, 3000)
},5000);
}(0));