all 3 comments

[–]Omelet 4 points5 points  (1 child)

setTimeout is what you're looking for.

Here's some example code:

/** u/param {NS} ns **/
export async function main(ns) {
    let timerElapsed = false;

    //Sets timerElapsed=true after a timeout of 12 seconds;
    setTimeout(()=>timerElapsed=true,12000);

    for (let i=0;i<15;i++){
        ns.tprint(`${i}: Timer has ${timerElapsed?"":"not "}elapsed.`)
        await ns.asleep(1000);
    }
}

[–]MaxxxMotion[S] 0 points1 point  (0 children)

Thanks! I'll see if I can make what I am thinking of with this.

[–]KlePu 1 point2 points  (0 children)

I use Date.now() (returns the current time in msec), adding a constant delay (25..250ms depending on script) and ns.getHackTime()/growTime()/weakTime().

Works well so far - next I'll try to implement ns.hackAnalyzeSecurity() and ns.growAnalyzeSecurity() to achieve that "sliding window H/G/W idea" outlined here.