trouble with SWITCH javascript always executing default case -
well have trouble , ive been dealing cant work
i have function
function getdirections(dir) { var img; switch(dir) { case 0: img+='n.png'; break; case 1: img+='ne.png'; break; case 2: img+='e.png'; break; case 3: img+='se.png'; break; case 4: img+='s.png'; break; case 5: img+='so.png'; break; case 6: img+='o.png'; break; case 7: img+='no.png'; break; default: alert('enetered default direction='+dir); } return img; }
quite simple right? have interval set 5000 ms call getdirections(variable), function works first time called after , enter in default clause , alerts 'entered default direction=dirvalue' , mean if dir value between 0-7 enter default: alert value supossed enter 1 of cases.
i made same using else if , worked dont know wrong switch
if(dir==0){img+='n.png';} else if(dir==1){img+='ne.png';} else if(dir==2){img+='e.png';} else if(dir==3){img+='se.png';} else if(dir==4){img+='s.png';} else if(dir==5){img+='so.png';} else if(dir==6){img+='o.png';} else if(dir==7){img+='no.png';}
that weird... try make sure dir int, before switch:
dir = parseint(dir);
if alert shows value correctly should enter switch, still can "look" correct of different data type. conversion manually ensure it's int
Comments
Post a Comment