I am attempting to use a callback to create an asynchronous function that does calculations behind a draw loop without slowing it down. I have read many callback examples and am clearly doing something wrong.
I have created a simplified version of what I would like to do. When you click your mouse it should do the math without hanging up the draw loop. Right now it causes a hangup:
var nPoints = 1;
var sumDone = false;
var sum = 0;
function setup() {
    var myCanvas = createCanvas(200, 200);
    myCanvas.parent("p5");
}
function draw(){
  nPoints = nPoints+1
  stroke(0,0,0);
  background(245,245,245);
  noFill();
  rect(1,1,198,198);
  textAlign(CENTER);
  fill(0,0,0);
  textSize(20);
  if(sumDone){
    text(sum,100,20);
  }else{
    text("not done",100,20);
  }
  noStroke();
  push();
  translate(100,100);
  rotate(nPoints/50);
  rect(-50,-10,100,20);
  pop();
  
}
function mouseClicked(){
  if(sumDone){
    sumDone = false;
    sum=0;
  }else{
    doMath(function (){
      sumDone = true;
    });
  }
}
function doMath(callback){
    for(var i=0;i<10000000;i++){
      sum = sum + i;
    }
  callback();
}<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
<body>
  <div id="p5" align="center">
  </div>
  <script>
    <!-- JAVASCRIPT CODE GOES HERE -->
  </script>
</body>As you can see, the math still completely hangs up the draw loop. Is it possible to do this in a way where the draw loop is not effected?
 
    