Time, delays, etc

  Since I've started programming in the first place I have always had an integer variable that would be used to trigger something at a certain moment. So in an infinite loop I would have something like 


    timeVariable++;
  If (timeVariable >= 500){
       doSomething ();
       timeVariable -= timeVariable;
}

The problem is I never learned how to properly keep track of time and I fear that the faster a computer is the faster this will trigger. I have always used this to create bullets, rain, etc. Would anyone mind showing me a proper way to keep track of time to trigger events (preferably in libgdx)?