0

How to run function anew (so that counter runs from 0 to specified number) each time I click corresponding to that function link ?

As for now the problem is that it works only on the first click.

https://jsfiddle.net/mtxg3ez2/56/

This is how I conncected 2 links with 2 different function names so that it fires : When I click the given link (there are 2 links) I store its data attribute inside variable and fire the function whose name matches that stored data attribute string.

Function:

HTML:

<a href="" data="one">one</a>

<a href="" data="two">two</a>

JS:

$('a').on('click', function(e) {
  e.preventDefault();
  var storedata = $(this).attr('data');

    jQuery('.canvaswrap').fadeIn();

  console.log(storedata)
  window[storedata]();


});


window.one = function() {
     setInterval(progressSim , 40); 

}

window.two = function() {

     setInterval(progressSim2 , 40); 
}

1 Answer 1

1

It would clearTimeout in progressSim() and progressSim2(), however sim and sim2 are undefined.

So you should assign the setInterval to a variable like:

var sim;
var sim2;
window.one = function() {
    sim = setInterval(progressSim , 40); 
}
window.two = function() {
    sim2 = setInterval(progressSim2 , 40); 
}

And reset the number to zero in

if (al >= 94) {
  al = 0; // reset the number
  clearTimeout(sim);
}

var ctx = document.getElementById('my_canvas').getContext('2d');
var ctx2 = document.getElementById('my_canvas2').getContext('2d');
var al = 0;
var start = 4.72;
var cw = ctx.canvas.width;
var ch = ctx.canvas.height;
var diff;

var greenPart = ctx.createLinearGradient(0, 0, 0, 100);
greenPart.addColorStop(0, '#0f2596');
greenPart.addColorStop(1, '#0ea5e8');

var whitePart = ctx.createLinearGradient(0, 0, 0, 100);
whitePart.addColorStop(0, '#fff');
whitePart.addColorStop(1, '#0e97df');

var width = 3;
var width2 = 1;

ctx2.save();
ctx2.beginPath();
ctx2.rect(-width, -width, 70 + width, 70 + width * 2);
ctx2.clip();
// Then we draw the left half

ctx2.beginPath();
ctx2.arc(35, 35, 45, 0, Math.PI * 4, false);
ctx2.fillStyle = '#fff';
ctx2.fill();

ctx2.restore();

// This is the First Function 

function progressSim() {
  diff = ((al / 100) * Math.PI * 2 * 10).toFixed(2);
  ctx.clearRect(0, 0, cw, ch);

  ctx.lineWidth = width;
  ctx.fillStyle = '#1c295c';

  ctx.textAlign = 'center';
  ctx.font = "bold 19px Arial";
  ctx.fillText(al + '%', cw * .54, ch * .54 + 2, cw);

  // First we make a clipping region for the left half
  ctx.save();
  ctx.beginPath();
  ctx.rect(-width2, -width2, 100, 100 + width2 * 2);
  ctx.clip();
  ctx.lineWidth = width2;
  // Then we draw the left half
  ctx.strokeStyle = "#d7ecf6";
  ctx.beginPath();
  ctx.arc(50, 50, 45, 0, Math.PI * 4, false);
  ctx.stroke();

  ctx.restore(); // restore clipping region to default

  // Then we make a clipping region for the right half
  ctx.save();

  ctx.beginPath();
  ctx.rect(50, -width, 50 + width, 100 + width * 2);
  ctx.clip();
  // Then we draw the right half
  ctx.strokeStyle = greenPart;
  ctx.beginPath();
  ctx.lineCap = 'round';
  ctx.arc(50, 50, 45, 4.78, diff / 10 + start, false);
  ctx.stroke();
  ctx.restore(); // restore clipping region to default
  // First we make a clipping region for the left half
  ctx.save();
  ctx.beginPath();
  ctx.rect(-width, -width, 50 + width, 100 + width * 2);
  ctx.clip();

  // Then we draw the left half
  ctx.strokeStyle = whitePart;
  ctx.lineCap = 'round';
  ctx.beginPath();
  ctx.arc(50, 50, 45, start, diff / 10 + start, false);
  ctx.stroke();

  ctx.restore(); // restore clipping region to default
  if (al >= 94) {
    al = 0; // reset the number
    clearTimeout(sim);
    // Add scripting here that will run when progress completes
  }
  al++;
}

// This is the Second Function 

function progressSim2() {
  diff = ((al / 100) * Math.PI * 2 * 10).toFixed(2);
  ctx.clearRect(0, 0, cw, ch);

  ctx.lineWidth = width;
  ctx.fillStyle = '#1c295c';

  ctx.textAlign = 'center';
  ctx.font = "bold 19px Arial";
  ctx.fillText(al + '%', cw * .54, ch * .54 + 2, cw);

  // First we make a clipping region for the left half
  ctx.save();
  ctx.beginPath();
  ctx.rect(-width2, -width2, 100, 100 + width2 * 2);
  ctx.clip();
  ctx.lineWidth = width2;
  // Then we draw the left half
  ctx.strokeStyle = "#d7ecf6";
  ctx.beginPath();
  ctx.arc(50, 50, 45, 0, Math.PI * 4, false);
  ctx.stroke();

  ctx.restore(); // restore clipping region to default

  // Then we make a clipping region for the right half
  ctx.save();

  ctx.beginPath();
  ctx.rect(50, -width, 50 + width, 100 + width * 2);
  ctx.clip();
  // Then we draw the right half
  ctx.strokeStyle = greenPart;
  ctx.beginPath();
  ctx.lineCap = 'round';
  ctx.arc(50, 50, 45, 4.78, diff / 10 + start, false);
  ctx.stroke();
  ctx.restore(); // restore clipping region to default
  // First we make a clipping region for the left half
  ctx.save();
  ctx.beginPath();
  ctx.rect(-width, -width, 50 + width, 100 + width * 2);
  ctx.clip();

  // Then we draw the left half
  ctx.strokeStyle = whitePart;
  ctx.lineCap = 'round';
  ctx.beginPath();
  ctx.arc(50, 50, 45, start, diff / 10 + start, false);
  ctx.stroke();

  ctx.restore(); // restore clipping region to default
  if (al >= 54) {
    al = 0; // reset the number
    clearTimeout(sim2);
    // Add scripting here that will run when progress completes
  }
  al++;
}


$('a').on('click', function(e) {
  e.preventDefault();
  var storedata = $(this).attr('data');
  jQuery('.canvaswrap').fadeIn();
  console.log(storedata)
  window[storedata]();
});
var sim;
var sim2;

window.one = function() {
  sim = setInterval(progressSim, 40);
}

window.two = function() {
  sim2 = setInterval(progressSim2, 40);
}
* {
  margin: 0;
  padding: 0;
}

a {
  display: inline-block;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 20px;
  margin: 10px 30px;
  color: teal;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="" data="one">one</a>
<a href="" data="two">two</a>
<div class="canvaswrap">
  <canvas id="my_canvas2" width="70" height="70"></canvas>
  <canvas id="my_canvas" width="100" height="100"></canvas>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

try to click fast 2 or more times on the given link, it picks up speed :P
So you have to clear both setInterval and reset the number when you clicked on one and two.
Something like so jsfiddle.net/c4khk69f/10 Dude thank you for your effort :)
Yes. Glad it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.