Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

timer is not working correclty

YamatoYamato Member Posts: 62 ✭✭ - Stalwart
I'm playing jester, trying to set a timer for bombs in html, and Its not working like I want it  to work.

here is my code:

var delayedAction = setTimeout(function() {
    set_variable("bomb_timer", get_variable("bomb_timer")-2)
    print("Time till BOOM = " + get_variable("bomb_timer"))
        if (get_variable("bomb_timer") > "0"){ run_function("setTimer")}
}, 2000);

aslong as the timer isn't 0 it should update the timer every 2 seconds, print "Time till BOOM =  + the time" and run it again, till it is 0.

this is the result
image
first, it continue's till -3 which is strange, also my timer reaches 0 after 10 seconds, (not 17)

any idea's why?

Comments

  • TrevizeTrevize Member Posts: 1,517 @ - Epic Achaean
    For one, "0" is a string, not a number.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • AntoniusAntonius Member Posts: 5,040 @@ - Legendary Achaean
    edited January 2014
    That doesn't actually cause any problems, the condition evaluates correctly since "0" will be converted to a number when doing the comparison.

    The code is correct, as far as I can see; I threw it into a JSFiddle, made some minor adjustments to remove the HTML5 client stuff, and it does pretty much exactly what I expected. I don't know if the HTML5 client functions are doing anything odd that may cause issues (I don't use the HTML5 client myself), but it seems like you could achieve the same thing using standard JavaScript variables unless you're doing other things with that bomb_timer client variable.

    Any chance you could post the full code of that setTimer function you have declared in your client's Function tab? I assumed that it was the code you've posted that is creating the setTimeout object, which was in turn calling setTimer again, but that may not actually be the case.
    Trevize
  • TrevizeTrevize Member Posts: 1,517 @ - Epic Achaean
    Antonius said:
    That doesn't actually cause any problems, the condition evaluates correctly regardless.

    The code is correct, as far as I can see; I threw it into a JSFiddle, made some minor adjustments to remove the HTML5 client stuff, and it does pretty much exactly what I expected. I don't know if the HTML5 client functions are doing anything odd that may cause issues (I don't use the HTML5 client myself), but it seems like you could achieve the same thing using standard JavaScript variables unless you're doing other things with that bomb_timer client variable.

    Any chance you could post the full code of that setTimer function you have declared in your client's Function tab? I assumed that it was the code you've posted that is creating the setTimeout object, which was in turn calling setTimer again, but that may not actually be the case.
    If not the string vs number, then yeah, at least in the script posted above I see no issues at all.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • YamatoYamato Member Posts: 62 ✭✭ - Stalwart
    well this is the full code.. 

    var delayedAction = setTimeout(function() {
        set_variable("bomb_timer", get_variable("bomb_timer")-1)
        print("Time till BOOM = " + get_variable("bomb_timer"))
            if (get_variable("bomb_timer") > "0"){ run_function("setTimer")}
    }, 1000);

    I did changed the "2000" into a "1000" for a 1 second delay, but other then this, you are looking at the full function.
  • YamatoYamato Member Posts: 62 ✭✭ - Stalwart
    edited January 2014
    I think I called the setTimer function a little too often.. I'm no scripting expert but,

    image

    at the 20.51.35 timeframe you can see the timer updated 5 times.

    basicly, I am trying to set 20 bomb timers all following eachother. with a bit of jester randomness.. (if you set a bomb's timer to 20 seconds, you can get 3 seconds more or less then intended)
  • AntoniusAntonius Member Posts: 5,040 @@ - Legendary Achaean
    Ok, are you using that bomb_timer variable in anything else or is it literally just for this function? I assume you're setting it as part of a trigger and then calling the setTimer function, but anywhere else?

    If not, you could do something like this instead:

    function setTimer(time) {
        var bombTime = parseInt(time, 10),
            bombTimer = setInterval(function() {
                bombTime -= 1;
                print("Time till BOOM = " + bombTime);
                if(bombTime <= 0)
                    clearInterval(bombTimer);
            }, 1000);
    }

    Then in your trigger you'd just do: run_function("setTimer", something); where something is whatever you use to get the actual number of seconds out of the trigger line.

    The major difference is that setInterval just keeps on executing on that delay until you cancel it. It's also not affected by the execution time of the function (though in most cases that should be pretty negligible anyway). I've also cut out the client variable using get_variable/set_variable, which may or may not have any effect whatsoever on speed/accuracy.

    One last question: Your computer does keep time properly, right?
  • YamatoYamato Member Posts: 62 ✭✭ - Stalwart
    edited January 2014
    I only use it for this trigger, and my computer keeps track of time correctly I assume.

    You set the bomb's timer for (\d+) seconds.

    if (args[1] < get_variable("bomb_timer")){
        set_variable ("bomb_timer", args[1]);
        }
    run_function("setTimer");

    this is the trigger that calls it. I'm guessing I ran the setTimer function a bit too often.
    is it possible to avoid this, and still set 20 bombs?
  • AntoniusAntonius Member Posts: 5,040 @@ - Legendary Achaean
    Oh, doing multiple bombs would definitely screw it up. Are you having issues with it when you just set a single bomb's timer?
  • YamatoYamato Member Posts: 62 ✭✭ - Stalwart
    a single timer works,
    I think I have a solution to the problem
  • YamatoYamato Member Posts: 62 ✭✭ - Stalwart
    edited January 2014
    result!
    only 8 bombs were blown, in my own hands

    image
    as you can see I had a brief moment of not being stunned arround 0:10:29
    at 0:10:33 I had one full second of freedom
    et voila! only 8 bombs.
Sign In to Comment.