Decrease int variable over time

Basically without going into too much detail about my project I’m writing a horror survival/3rd person shooter. (its kind of top secret) lol. I have a flashlight that I got working so far when f is pressed it turns on/off plays a little clicking sound, but now I want to add battery power. Kind of like if flashlight is turned off drain power by 5 every 5 seconds, and what not. when its turned off increase power by 5 every 5 seconds if it goes below 1 disable until full again. etc. I can do the code I’m just a little lost at how I would start with decreasing the power variable if flashlight is enabled over time.
the code I have so far is this (its in JS btw)

var flashlight : Light;

var flashlightsound : AudioClip;

var flashlightMaxPower = 100;

var flashlightCurPower = 100;

function Update () {

if (Input.GetKeyDown(KeyCode.F)){

flashlight.enabled = !flashlight.enabled;

audio.clip = flashlightsound;

audio.Play();

}

// Write the code for the battery power

}

any help would be much apreciated.

Or if you want a more optimized solution than Time.deltaTime, use invokes.

var flashlight : Light;
var flashlightMaxPower : int = 100;
var flashlightCurPower : int = 100;

function Start () {
    InvokeRepeating("FlashlightPower", 5, 5);
}

function FlashlightPower () {
    if (flashlight.enabled) {
        if (flashlightCurPower>0) 
            flashlightCurPower-=5;
        } else {
            flashlight.enabled = false;
        }
    } else {
        if (flashlightCurPower<flashlightMaxPower)+=5;
    }
}

It would be easiest if you converted your flashlightCurPower to a float value. By doing so can easily reduce the float by Time.deltaTime, which will result in a decrease of 1 ever seconds. You cannot decrease a integer value with Time.deltaTime since it is of a float value.

var flashlightCurPower : float = 100;

    function Update (){
        if(flashlight is on){
            flashlightCurPower -= Time.deltaTime;
        }
        flashlightCurPower = Mathf.Clamp(flashlightCurPower, 0 flashlightMaxPower); // Make sure the battery level does not become less than 0 or max than 'flashlightMaxPower'
    }

Some thing like that would my way to go.

Use floats and use this:

   if (flashlightCurPower > 0) 
        flashlightCurPower = flashlightCurPower - 5f * (60f * Time.deltaTime);
    } else if (flashlightCurPower <= 0) {
        flashlight.enabled = false;
    }

To give you a point to get started, look at time.deltaTime. It will give you the time that has passed since the last update. If you are wanting to decrement power by 5 every 5 seconds, create a variable to store elapsed time and increment it by time.deltaTime each update, then check to see if it is > 5, if so, decrement your power, and reset your variable back to 0.

Hope this helps,
-Larry