Ashish Ashish - 28 days ago 7
Javascript Question

websocket.js:120 Uncaught ReferenceError: insideFun is not defined when I added function inside the function

I am getting coordinates data via Web Socket.



ws.onmessage = newmessage;
function newmessage (event) {
var mySpan = document.getElementById("messageGoesHere");
var mySpan2 = document.getElementById("messageGoesHere2");
var str = event.data;





After splitting the coordinates I am adding that values into draw function



var array = str.split('|');
if((array[1].localeCompare('xy'))==0)
{
mySpan.innerHTML = parseInt(array[2]);
mySpan2.innerHTML = parseInt(array[3]);
draw(parseInt(array[2]), parseInt(array[3]));
}
else if((array[1].localeCompare('con'))==0)
{
checkData(parseInt(array[2]), parseInt(array[3]), parseInt(array[4]))
}





Now I want use the drawDrid function inside the draw function, if it is possible or kindly suggest me some another solution.



function draw(a, b){
console.log("a :" +a+"b :"+b);
ctx.fillStyle = "#000";
a = a * gridBoxLength;
b = b * gridBoxLength;
ctx.fillRect(a, b, gridBoxLength, gridBoxLength);
function checkData(x, y, z){
console.log("x :"+x+"y :"+y+"z :"+z);
}
}




Answer

Try this code , maybe this will help you. If you want to use a function inside the function you have to window for accessing data outside the function and this is the only way to access outside function data.

$(document).ready(function(){

window.newFunction =  function (a,b,c,d,e)
 {
  window.data = [a,b,c,d,e];
  //code 
 }
});