user2283907
user2283907

Reputation: 13

call JS function only once

I'm working with the following function which runs fine but I only want to call it once - currently every time I use scroll, the function id triggered and as a result my animated charts get re-built.

window.onscroll = function() {


new Chart(document.getElementById("doughnut").getContext("2d")).Doughnut(doughnutData);
new Chart(document.getElementById("line").getContext("2d")).Line(lineChartData);
new Chart(document.getElementById("radar").getContext("2d")).Radar(radarChartData);




}

thanks!!!!

Upvotes: 0

Views: 4416

Answers (1)

OzrenTkalcecKrznaric
OzrenTkalcecKrznaric

Reputation: 5646

var isNotScrolled = true;

window.onscroll = function() {
  if(isNotScrolled)
  {
    new Chart(document.getElementById("doughnut").getContext("2d")).Doughnut(doughnutData);
    new Chart(document.getElementById("line").getContext("2d")).Line(lineChartData);
    new Chart(document.getElementById("radar").getContext("2d")).Radar(radarChartData);

    isNotScrolled = false;
  }
}

You may also consider using jQuery with waypoint plugin, that may help you doing exactly what you want.

Upvotes: 3

Related Questions