Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Morning All,

I have one query regarding increment of for-loop after 1 minute interval, i.e like if I have 5 elements in an Array and I want to execute all them with 1 minute interval.
Please help me how can i achieve it.
Posted
Comments
Philip Stuyck 1-Aug-12 4:43am    
This is a javascript question right ?
Cause I see .NET answers here below as well.

You don't need a real for loop.
You need a timer that expires every minute.
When it expires you increment an internal variable that you use as index into the array.

For example in client side javascript you have 2 methods of the window object:
setTimeout : schedules a function to run after a specified number of miliseconds
setInterval : is the same but invokes the function repeatedly :var h = setInterval(callback,60000)
clearInterval : can be used to clear the timer : clearInterval(h)
 
Share this answer
 
v2
C#
Class CustomTimer
{ 
   private DateTime startTime;
    private DateTime stopTime;
    private bool running = false;

    public void Start() 
    {
        this.startTime = DateTime.Now;
        this.running = true;
    }

    public void Stop() 
    {
        this.stopTime = DateTime.Now;
        this.running = false;
    }

    //this will return time elapsed in seconds
    public double GetElapsedTimeSecs() 
    {
        TimeSpan interval;

        if (running) 
            interval = DateTime.Now - startTime;
        else
            interval = stopTime - startTime;

        return interval.TotalSeconds;
    }

}


Now within your foreach loop do the following:
C#
CustomTimer ct = new CustomTimer();
    ct.Start();
    // put your code here
    ct.Stop();   
  //timeinsecond variable will be set to time seconds for your execution.
  double timeinseconds=ct.GetElapsedTime();
 
Share this answer
 
You can use a long dummy for loop for defined interval OR use a JavaScript timer.

For timer, refer: Javascript timers[^]
 
Share this answer
 
Set a timer as below to execute query in a regular interval

Int Counter=0;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,60);



dispatcherTimer.Start();



private void dispatcherTimer_Tick(object sender, EventArgs e)
{
write your query and code here......
and set a counter to stop the timer when you want
Counter++;
if(Counter==5)
dispatcherTimer.Stop();

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900