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
first, it continue's till -3 which is strange, also my timer reaches 0 after 10 seconds, (not 17)
any idea's why?
Comments
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.
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.
Results of disembowel testing | Knight limb counter | GMCP AB files
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.
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?
Results of disembowel testing | Knight limb counter | GMCP AB files
Results of disembowel testing | Knight limb counter | GMCP AB files