javascript - Something horribly wrong with my js code, anyone can help me debug? -
i'm trying write tool, animate game map range of dates. flow this:
1st: choose game world
2nd: set map display parameters (date range, map type , animation speed)
3rd: js code grab png file according dates , display them 1 one according animation speed
the problem i'm having this: if click on 1 world, , click animate, fine, animation displays correctly. if choose world (without refreshing page), animation either flickers or somehow displaying image other worlds. , can't figure out what's causing (i'm totally n00b @ js)
$(function(){ $("#image_area").hide(); $("#tw_image").hide(); $('#w40').click(function(){ $("#tw_image").hide(); show_image_area('40'); }); $('#w42').click(function(){ $("#tw_image").hide(); show_image_area('42'); }); $('#w56').click(function(){ $("#tw_image").hide(); show_image_area('56'); }); }); function show_image_area(world){ $("#tw_image").hide(); if(world == "40" || world == "42" || world == "56"){ $("#map_notice").remove(); $("#image_area").prepend('<div id="map_notice">map w'+world+' available <span id="date_highlight">april 7th 2011</span>, previous dates invalid , not have map available</div>'); } $("#date_from").datepicker({ showanim: 'blind' }); $("#date_to").datepicker({ showanim: 'blind' }); $('#image_area').show(); $("#animate").click(function(){ $("#tw_image").hide(); var date_from = $("#date_from").val(); var date_to = $("#date_to").val(); if(!(date_from && date_to)){ alert("from , dates required.") return false; } var map_type = $("#map_type").val(); var speed = $("#speed").val(); var days = daydiff(parsedate(date_from), parsedate(date_to)); var date_one = new date(date.parse(date_from)); var b = date_one.toisostring().split("t")[0].split("-"); var c = get_map_type(map_type) + b[0] + b[1] + b[2]; var width = get_map_type_width(map_type); var img_load = "" + world + "/" + c + ".png"; $('#image_area').load(img_load, function(){ $("#tw_image").attr("width", width); $("#tw_image").attr("src", img_load); $("#tw_image").show(); $("#debug").html("world = "+world); }); var = 0; var interval = setinterval( function(){ date_one.setdate(date_one.getdate() + 1); b = date_one.toisostring().split("t")[0].split("-"); c = get_map_type(map_type) + b[0] + b[1] + b[2]; var src_one = "" + world + "/"+c+".png"; var img = new image(); img.src = src_one; img.onload = function(){ $("#tw_image").attr("src", img.src); $("#debug").html("world = "+world); } i++; if(i >= days) clearinterval(interval); },speed); }); } function get_map_type(map_type){ if(map_type == "topk"){ return "topktribes"; } if(map_type == "toptribe"){ return "toptribes"; } if(map_type == "topnoble"){ return "topnoblers"; } } function get_map_type_width(map_type){ if(map_type == "topk"){ return 1000; } if(map_type == "toptribe"){ return 1300; } if(map_type == "topnoble"){ return 1300; } } function parsedate(str) { var mdy = str.split('/'); return new date(mdy[2], mdy[0]-1, mdy[1]); } function daydiff(first, second) { return (second-first)/(1000*60*60*24) }
ok think have solution here although not thought going be. every time calling image_area_world creating new click handler on animate button. due way javascript scope works world variable kept same click handler @ point of creation.
anyway solve problem can try before define click handler.
$("#animate").unbind("click"); $("#animate").click(function () { *code* } a couple of tools out.
also bit explaining how javascript scope , closures work
Comments
Post a Comment