Javascript function executes twice, Meteor js -
this simple script horizontal fortune roulette, created using slick slider. problem: function rollout() executed twice (and helpers too) because template rendered twice. i'm using iron:router routing, code:
router.configure({ loadingtemplate: "loading", layouttemplate: "layout" }); router.map(function() { this.route("hello", { path: "/", waiton: function() { return meteor.subscribe("games"); } }); });
hello.js:
session.setdefault('currentslide', 0); template.hello.events({ 'click .create': function(e) { e.preventdefault(); var ins = { gamestarttime: new date(), finished: false } games.insert({ gamestarttime: new date(), finished: "false" }); session.set("gamestatus", "waiting"); } }) template.hello.helpers({ currentslide: function() { var value = session.get('currentslide'); if (value === 25) { return 0; } return value; }, games: function() { var game = games.find(); return game; }, gamehandler: function() { var game = games.findone(); var gamestatus = session.get("gamestatus"); if (game && game.finished === "false" && gamestatus === "waiting") { games.update({_id: game._id}, { $set: {running: true} }); } else if (gamestatus === "finished") { meteor.call('updategame', game._id); //remove item collection after game over; error here: typeerror: cannot read property '_id' of undefined; console.log('game finished'); } if (game && game.running === true) { //set session variable , launch roulette session.set("gamestatus", "running"); rollout(game._id); } } }); var rollout = function(gameid) { session.set("gamestatus", "running"); //make div's visible. have display:none default. document.getelementbyid("roulette").style.display = "block"; document.getelementbyid("result").style.display = "block"; document.getelementbyid("wrapper").style.display = "none"; var speed = 500; //default slider speed session.setdefault('speed', speed); var r = $('#roulette').slick({ centermode: true, slidestoshow: 7, slidestoscroll: 1, autoplay: true, autoplayspeed: 0, speed: speed, draggable: false, pauseonhover: false, cssease:'linear' }); var maximum = 14; var minimum = 0; var randomnumber = math.floor(math.random() * (maximum - minimum + 1)) + minimum; var winresult = randomnumber; var speed = 80; var currentslide = session.get('currentslide'); function roll(callback) { session.set('currentslide', r.slickcurrentslide()); if (speed < 800) { speed += 20; r.slicksetoption("speed", speed, false); settimeout(function() { roll(callback); }, 500); } else if (speed >= 600 && speed < 1300) { speed += 40; r.slicksetoption("speed", speed, false); settimeout(function() { roll(callback); }, 300); } else if (speed >= 1300 && speed < 20000) { settimeout(function() { speed += 3; r.slicksetoption('speed', speed, false); if (session.get('currentslide') === winresult) { r.slickpause(); settimeout(function() { //hide roulette div's document.getelementbyid("roulette").style.display = "none"; document.getelementbyid("result").style.display = "none"; document.getelementbyid("wrapper").style.display = "block"; r.unslick(); session.set('currentslide', 0); session.set("gamestatus", "finished"); callback(true); return }, 10000); } else { roll(callback); } }, 30) } else { callback(true); } } roll(function(callback) { meteor.call('updategame', gameid); //remove item collection session.set("gamestatus", "finished"); }); }
in template i'm using spinner sacha:spin package , working fine.
the gamehandler function should not in template helpers, meant supply information template. instead, logic needs moved autorun function. autorun statement re-run anytime reactive datasource within changes.
ex:
template.hello.oncreated(function() { this.autorun(function() { var game = games.findone(); var gamestatus = session.get("gamestatus"); // ...etc }); }
Comments
Post a Comment