StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Tatarize  
#1 Posted : Saturday, November 27, 2021 1:51:12 AM(UTC)
Tatarize

Rank: Member

Reputation:

Groups: Approved
Joined: 4/13/2021(UTC)
Posts: 8
United States

Thanks: 4 times
Was thanked: 1 time(s) in 1 post(s)
I want to have a script execute at a random time during the 4AM hour.

While schedules aren't supported by the sp.Timers() it was easy enough to use timers to set an event.

In 2.1 hours start 1 day timer. But, two things occur to me:

1) This timer will not survive a reboot. I'll need to fix it for the next time and even though sp.net starts with the system it won't trigger automatically fix this, since it doesn't know what time it is or how many hours to the 4AM hour.
2) I do not want the event to not occur at exactly the same time. So I'd like it to trigger off another timer that will happen sometime during the hour.

I figured I'd try math.Random() but no dice... Am I missing something extremely helpful here or some other way of accomplishing this task?
Rob  
#2 Posted : Saturday, November 27, 2021 4:41:41 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
I haven't actually tested any of this, but in theory it should work, or at least be close to functional.
Though if S+ starts during the 4AM hour, it will end up creating a timer to run the next day (not knowing whether one for the current day ran or not) - so you may need to refine to help cover gaps, but making sure one timer doesn't end a create another within the same hour for example. You could create a file with the last run date string and check that, as one example.

Hopefully this makes sense!

Copy this into your Global Actions > Load/Unload > Load script:
Code:
//Calculates the next time to run and creates the timer
function Create4AMTimer() {
    var curDate = new Date();
    var curHour = currTime.getHours();
    var rndMinute = Math.floor(Math.random() * 60);
    var nextRunString = '';
    if(curHour < 4) {
        //Today, start timer soon
        nextRunString = curDate.getFullYear() + '-' 
                      + (curDate.getMonths()+ 1) + '-'
                      + curDate.getDate() + ' '
                      + '04:' + rndMinute + ':00';
    } else {
        //Start timer for tomorrow in the 4AM area
        var tomorrowDate = new Date();
        tomorrowDate.setDate(curDate.getDate()+1);
        nextRunString = tomorrowDate.getFullYear() + '-' 
                      + (tomorrowDate.getMonths()+ 1) + '-'
                      + tomorrowDate.getDate() + ' '
                      + '04:' + rndMinute + ':00';
    }
    
    var nextRun = new Date(nextRunString);
    var millisecondsUntilNextRun = nextRun.getTime() - curDate.getTime();

    //Create timer to run at the next time window within 4AM
    //Interval is -1 so it only runs one, new timer is created
    //upon execution of task
    sp.CreateTimer("Daily4AMish", 
                    millisecondsUntilNextRun, -1, 
                    "Execute4AMTask();");
}

//Executes your code, then creates a new timer for tomorrow
function Execute4AMTask() {
    //Your code here
    

    //Start next timer
    Create4AMTimer();
}


//On load, create the first timer
if(sp.GetTimer("Daily4AMish") == null) {
    Create4AMTimer();
}
thanks 1 user thanked Rob for this useful post.
Tatarize on 11/27/2021(UTC)
randomConstant  
#3 Posted : Saturday, November 27, 2021 4:53:02 AM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
You can get system time using code below:
Code:
DateTime.Now.ToString("yy/MM/dd HH:mm");

You can get current system hour using code below:
Code:
DateTime.Now.ToString("HH");

Feel free to edit the format, following link provides details about DateTime:
Custom Date & Time Format Link

One possible way of doing this might be to make a timer script that checks if time is 04:00 (putting only HH in the DateTime format will return only hours (eg. 04) which you can compare as a string), if its true the timer can start the required script and if false the timer can start the timer script (itself) again, time duration can be hourly or less depending on your usecase.

You can run any script automatically as soon as S+ starts with windows or reloads by putting your script in Global Actions > Load/Unload > Load section (enable it and put script in)

Hopefully this helps and if not then Rob is always here : )

thanks 1 user thanked randomConstant for this useful post.
Tatarize on 11/27/2021(UTC)
randomConstant  
#4 Posted : Saturday, November 27, 2021 4:56:32 AM(UTC)
randomConstant

Rank: Advanced Member

Reputation:

Groups: Translators, Approved
Joined: 7/17/2021(UTC)
Posts: 135

Thanks: 35 times
Was thanked: 18 time(s) in 15 post(s)
Well Rob already replied as I was writing my post. Razz

Also just noticed you would need to run timer script no matter if the (hour == 04) condition is true or not because you need it running for the next time time becomes 04, also put timer delay 1 hour to avoid running the script multiple times in the same hour.
Tatarize  
#5 Posted : Saturday, November 27, 2021 2:38:30 PM(UTC)
Tatarize

Rank: Member

Reputation:

Groups: Approved
Joined: 4/13/2021(UTC)
Posts: 8
United States

Thanks: 4 times
Was thanked: 1 time(s) in 1 post(s)
This certainly has the right pieces I need.

The reference to currTime needs to be curDate.getHours() and getMonths() should be getMonth(). But, other than that this seems like it would work. Though this is probably better off as two stage.

Just set it to run exactly at 4:00am. And the code it runs is a timer to run the real code that gets issued somewhere randomly within the next (1000 * 60 * 60) milliseconds.

It does correct the missing bits I needed. I tried Math.random() but it wouldn't take it since it was turning the time before running the timer into a float. But, appending ~~ to the front there forces it to look like an int.

I assume -1 for the interval makes it not repeat.

---

If I kick it off exactly at 4AM I can just run it as a day long timer, that kicks off the random minute timer. Though daylight savings time might screw me up.

Code:

function CreateTimeTimer(hours, minutes, task) {
    var curDate = new Date();
    var curHour = curDate.getHours();
    var curMinutes = curDate.getMinutes();
    var minutesUntilTime = ((hours * 60) + minutes) - ((curHour * 60) + curMinutes);
    if (minutesUntilTime <= 0) {
        minutesUntilTime += 24 * 60
    }
    msUntilTime = minutesUntilTime * 60 * 1000
    sp.CreateTimer('TimeTimer',
                    ~~msUntilTime,
                    24 * 60 * 60 * 1000,
                    task);
}

function RandomPeriodTimer(hours, minutes, task) {
    var rand = ((hours * 60) + minutes) * 60 * 1000 * Math.random()
    sp.CreateTimer('RandomPeriod',
                   ~~rand,
                   -1,
                   task);
}



sp.DeleteAllTimers();
CreateTimeTimer(4,00, "RandomPeriodTimer(0,60,\"TestDisplay()\");");

function TestDisplay() {
    sp.MessageBox('Test', 'Timer');
}

Edited by user Monday, November 29, 2021 2:16:12 PM(UTC)  | Reason: Update names, change variable around

Tatarize  
#6 Posted : Saturday, November 27, 2021 2:46:16 PM(UTC)
Tatarize

Rank: Member

Reputation:

Groups: Approved
Joined: 4/13/2021(UTC)
Posts: 8
United States

Thanks: 4 times
Was thanked: 1 time(s) in 1 post(s)
That's annoying. Editing my own post forces it get approval and unposted what I wrote? Apparently I need to delete my posts and post them again with corrections.
Rob  
#7 Posted : Saturday, November 27, 2021 3:02:02 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Originally Posted by: Tatarize Go to Quoted Post
That's annoying. Editing my own post forces it get approval and unposted what I wrote? Apparently I need to delete my posts and post them again with corrections.


I'll bump up your rep so you don't have that. Always about preventing spammers.
Tatarize  
#8 Posted : Monday, November 29, 2021 2:21:39 PM(UTC)
Tatarize

Rank: Member

Reputation:

Groups: Approved
Joined: 4/13/2021(UTC)
Posts: 8
United States

Thanks: 4 times
Was thanked: 1 time(s) in 1 post(s)
Originally Posted by: Rob Go to Quoted Post
Originally Posted by: Tatarize Go to Quoted Post
That's annoying. Editing my own post forces it get approval and unposted what I wrote? Apparently I need to delete my posts and post them again with corrections.


I'll bump up your rep so you don't have that. Always about preventing spammers.


Oddly still seems to have done that. It seems like the permissions to edit your own post should be about the same as to delete it and post again.

Rob  
#9 Posted : Monday, November 29, 2021 3:33:27 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Hmm, I noticed there's a separate moderation level for each individual forum, it's based on post counts (perhaps per forum?). I've reduced those, will see how it goes. I know that folks with multiple top-level posts end up going straight through, only less active or new accounts get stopped for moderation.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.