We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

set-interval and clear-interval inside for loop

Hi all ,

I have a set-interval function inside a for loop and when inside the set-interval function if it meets a criteria give an alert and clear the interval. Below is my code but its not working can anyone tell what the mistake here.

var timeCheck = 0;
function matchTime() {
    for (var i=0;i<timers.length;i++) {
        timeCheck = setInterval(function () {
            var theDate = new Date(timers[i][0]*1000); 
            var now = new Date(); 
            if ( (now.getFullYear() === theDate.getFullYear()) && (now.getMonth() === theDate.getMonth()) ) {
                if ( (now.getDate() === theDate.getDate()) && (now.getHours() === theDate.getHours()) ) {
                    if ( now.getMinutes() === theDate.getMinutes() && (now.getSeconds() === theDate.getSeconds()) ) { alert("its Time for "+timers[i][1]); stopCheck(); }
                }
            }
        }, 10);
    }
}

function stopCheck() { clearInterval(timeCheck); }

Thanks.

What I am trying to solve is : I need to get an alert every-time when the local time matches the time in the timers array (column 0; timers[count][0]). The array is already sorted
timers.sort(function(a,b) { return a[0] - b[0]; });

2
Contributors
1
Reply
4 Hours
Discussion Span
2 Months Ago
Last Updated
10
Views
Question
Answered
ckjaseem
Newbie Poster
7 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Self-Answered as of 2 Months Ago

set-interval and clear-interval inside for loop

Can you at least explain how you solve the loop? A simple explanation you don't need to provide a code if you don't want too. Just an explanation will be fine.

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0702 seconds using 2.68MB