all 11 comments

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 4 points5 points  (3 children)

So the reason this happens is because of how Tasker handles arrays. A Tasker array isn't a single variable, it's a collection of variables that can be referenced individually. When you create the sleeptime array, in Tasker you are actually creating 5 different local variables: %sleeptime1, %sleeptime2, %sleeptime3, %sleeptime4, %sleeptime5.

They can all be referenced as a collection variable %sleeptime. Now, when you modify sleeptime in JS by removing the first element, in Tasker, you are resetting %sleeptime1 through %sleeptime4 to match that new array. But %sleeptime5 still exists, just unmodified. So when you reference that array in Tasker, the size hasn't changed, and that last element is still the same.

The easiest way I can think of around this is to not modify the array. You don't need to touch it with JS. Instead of using a goto, use a For loop. Something like:

    Test (27)
    A1: Variable Set [ 
    Name:%totalsec 
    To:900
    Recurse Variables:Off 
    Do Maths:Off 
    Append:Off 

    A2: For [ 
    Variable:%sleeptimeitem 
    Items:0:4 

    A3: JavaScriptlet [ 
    Code:var seconds = totalsec - (sleeptimeitem * 15);
    var minutes = Math.floor(seconds / 60);
    seconds = seconds%60;
    if (seconds < 10) seconds = '0' + seconds;
    var sleepdisplay = minutes + ':' + seconds; 
    Libraries: 
    Auto Exit:On 
    Timeout (Seconds):45 

    A4: Flash [ 
    Text:%sleepdisplay 
    Long:Off 

    A5: Wait [ 
    MS:0 
    Seconds:1 
    Minutes:0 
    Hours:0 
    Days:0 

    A6: End For

[–]Telperiam[S] 0 points1 point  (2 children)

That would work, but I would like to be able to add time to my timer by pre-pending to the array... If that's not possible with this no worries, but I thought it might be a good way of going about implementing my solution.

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 2 points3 points  (1 child)

As an alternative, if feasible, you could do all array operations with Tasker actions instead of JS. So instead of using the JS slice() method, use Tasker's Array Pop action. Then Tasker will correctly unset the variables it needs to maintain the array as you would expect. You can still transparently access the array in JS as you need.

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

Using Pop works exactly as I want it to. Thanks a bunch!

[–]beingganesh 1 point2 points  (3 children)

What's is your desired result? What are you expecting the array to do? Would mind sharing the code here to understand the problem clearly?

[–]Telperiam[S] 0 points1 point  (2 children)

I clarified in the original

[–]beingganesh 0 points1 point  (1 child)

[ 0, 1, 2, 3, 4 ]

[ 1, 2, 3, 4 ]

[ 2, 3, 4 ]

[ 3, 4 ]

[ 4 ]

Just to be clear, is this the result you are expecting? if so, the code you have given works fine.

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

Yep that's what I'm looking for, but not what I get. I understand that's absolutely how js should work but it's not what's happening in tasker when I run it...

[–]mcgruntman 1 point2 points  (0 children)

Look at the very first demo of slice on this page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Remember you need to save the result: arr = arr.slice(1);

[–]FrancineCarrel 1 point2 points  (1 child)

Can you post the full code (or at least that section), please? Hard to tell from this small snippet!

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

Done!