1

I am calling a method in Javascript every 1 sec using setInterval, when google tab is inactive this function is not called or slows down. This function is for timer countdown, even when Tab is inactive I want setInterval to call my function every 1 sec. I have seen similar questions and answers here in this forum, but those solutions did not work for me. I am fairly new to using Javascript. Here's my code:

<div id ="clock"/>
self.setInterval("timer()",1000); 
var timeSpentSecs = 0;
var timeLimit = 300000;
function timer()
{
     var timeLeft = timeLimit - timeSpentSecs; 
     var timeLeftHour = Math.floor(parseInt(timeLeft)/3600); 
     timeLeft = timeLeft % 3600; 
     var timeLeftMin = Math.floor(parseInt(timeLeft)/60); 
     var timeLeftSec = timeLeft % 60; timeSpentSecs++;                   
     document.getElementById("clock").innerHTML = "<B>" + ' Timeleft ' + timeLeftHour + ' :' + timeLeftMin + ' :' + timeLeftSec + "</B>";
 }
Derek
  • 952
  • 3
  • 13
  • 34
Raji
  • 11
  • 1
  • Is your Javascript just floating in your HTML? If so, that's why its not working. – thatidiotguy Aug 23 '13 at 19:12
  • 1
    You need to use a date object – mplungjan Aug 23 '13 at 19:14
  • 2
    *"I have seen similar questions and answers here in this forum, but those solutions did not work for me"* What have you tried and what didn't work exactly? – Felix Kling Aug 23 '13 at 19:14
  • @FelixKling I was trying to use wrong solutions for me like, Solutions similar to this link from StackExchange: http://stackoverflow.com/questions/6951727/setinterval-not-working-properly-on-chrome and http://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab-is-inactive-in-chrome – Raji Aug 24 '13 at 00:53
  • And why were these "wrong"? What happened and what did you expect to happen. If you don't explain properly what the problem is, then we cannot help you. The question has been asked before, but if that solution didn't work for you, you have to explain *exactly* why. The information you provide, the easier it is for use to help you and everyone is happy. – Felix Kling Aug 24 '13 at 06:49

0 Answers0