Repeat function with interval

This is a discussion on Repeat function with interval within the C++ Programming forums, part of the General Programming Boards category; I want a function to be executed in a while-loop with intervals of x seconds. Here's what I've got: Code: ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Repeat function with interval

    I want a function to be executed in a while-loop with intervals of x seconds.

    Here's what I've got:

    Code:
    while (1)
    {
      if (loopcount == 0)
          {
            function();
            loopcount++;
          }
    
          if (loopcount == 1)
          {
              int milliseconds = 3000;
              long timeout = clock() + milliseconds;
              if(clock() < timeout) 
              {
                // nothing
              }
              else
              {
                  loopcount--;
              }
          }
    }
    No error, but it's not working! I tried a lot of things and I've read http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well considering you're reassigning the variables everytime you loop. You're basically trying to catch up with a time that's increasing as fast as you're getting to it.

    Code:
    int milliseconds = 3000;                // Outside of
    long timeout = clock() + milliseconds;  // the loop
    
    while(1) /*shudder*/ {
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21