-4
function active_timer(){
    var time = 5000;

    interval = setInterval(function(){
        console.log('interval');
    },time);
}

active_timer();

socket.on('timer', function (data) {

    console.log('here') // it triggered

    clearInterval(interval); // don't work
    active_timer() //resume
});

I tried this and it won't work because the console.log still triggered every 5 sec. Any clue why?

Soviut
  • 88,194
  • 49
  • 192
  • 260
Maria Jane
  • 2,353
  • 6
  • 23
  • 39
  • 50000 is 50 seconds. Remove the call again and see if it works – Andrew Li Sep 10 '16 at 16:24
  • 4
    Either because you are calling `active_timer` right after you cleared the interval or because the `'timer'` event is never triggered. – Felix Kling Sep 10 '16 at 16:25
  • On timer you clear interval and then start it again. Pass interval value to console log. Is it the same? – The Witness Sep 10 '16 at 16:25
  • @FelixKling clear the current interval and '`reset'` it, what's wrong with that? – Maria Jane Sep 10 '16 at 16:27
  • Nothing. But you are asking why the `console.log` continues to be shown. It continues because *you* are calling `active_timer` again, which starts the interval again. Stopping the interval and starting it again makes it look like it never stops. What other behavior would you expect? – Felix Kling Sep 10 '16 at 16:28
  • @MariaJane If you clear it and reset it, that may be the 'problem' your experiencing? It's just happening over and over again. It should be clearing, but you just redo the interval... – Andrew Li Sep 10 '16 at 16:28
  • 1
    It can't be that hard to understand? You have an interval that logs something every five seconds. You then stop that interval, but within milliseconds you call the same function once again, and the interval keeps running again, still logging to the console every five seconds. The line right after `clearInterval` calls the function again, starting the same interval over again. – adeneo Sep 10 '16 at 16:35
  • @FelixKling, here's what I expected : 1) interval triggered, says it count till 3 seconds, and then the `timer` socket triggered 2) hence the interval `resetted` – Maria Jane Sep 10 '16 at 16:35
  • "resetted" how, `time` is still five seconds? If you want to just ***pause*** the interval and start where it left of, that's something else. – adeneo Sep 10 '16 at 16:36
  • @adeneo the problem is it doesn't `'pause'`, i want to active_timer to pause and resume or reset, not still continue, that's why I use clearInterval and set the timer, expecting it will got reseted – Maria Jane Sep 10 '16 at 16:37
  • @adeneo yes.... – Maria Jane Sep 10 '16 at 16:37
  • http://stackoverflow.com/questions/21277900/javascript-pausing-setinterval – adeneo Sep 10 '16 at 16:38
  • *"i want to active_timer to pause and resume or reset, not still continue"* How do you measure that it "just" continues? Is the following `console.log` really shown exactly after 5 seconds? In theory it will be anytime between 5 and 10 seconds, since the the previous execution of the interval might just have happened or is about to happen when `clearInterval` is called. You claim that "it doesn't work" but you are not providing any evidence for that claim. I'm sure it does work, but it might not be what you want, in which case you have to properly explain what you want. – Felix Kling Sep 10 '16 at 16:42
  • 1
    You should `console.log(interval)` rather than `'interval'` string, because you'll see that the number that references your interval timer keeps changing. It looks like it's not stopping, but it actually is, over and over, without any noticeable gap. Logging the `interval` number will let you see that it's different over time. – Soviut Sep 10 '16 at 16:43

1 Answers1

0

Your code is technically running correctly but your console logs are misleading.

You've started an interval which triggers every 5 seconds. Each time your socket even fires, you clear the interval but then immediately start a new one. This gives the illusion that there's no gap in the intervals, but there is.

You can test this by changing your console.log('interval') to console.log(interval). So instead of just printing "interval", it will print out the value of the variable referencing the interval object. This is represented by a number which will change each time the interval is stopped and restarted.

Just to be clear, an interval cannot be resumed, only new ones started and existing ones stopped.

var interval = null;

function startTimer() {
  // store the reference to our interval
  interval = setInterval(function() {
    console.log('triggered interval', interval);
  }, 2000);
  
  console.log('new interval started', interval);
}

startTimer();

// button click to simulate your socket event
$('#simulate').on('click', function() {
  console.log('received simulated event');
  clearInterval(interval);
  startTimer();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="simulate">Simulate Socket Event</button>
Soviut
  • 88,194
  • 49
  • 192
  • 260