From 09995d5a216fb78c33787773b2fe069492e51190 Mon Sep 17 00:00:00 2001 From: deltab Date: Fri, 18 Aug 2017 19:06:02 +0100 Subject: [PATCH] Avoid setting up a timer if it's unneeded This change checks whether there are any `updating-chart` elements on a page before setting up the timer that updates them. Obviously the check is unnecessary on *this* page, but the demo script gets copied into themes etc. and ends up loaded on pages that don't have any updating charts, wasting CPU time and electricity. --- index.html | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 45d850f..d190596 100644 --- a/index.html +++ b/index.html @@ -63,16 +63,18 @@ // Updating charts. var updatingChart = $(".updating-chart").peity("line", { width: 64 }) - setInterval(function() { - var random = Math.round(Math.random() * 10) - var values = updatingChart.text().split(",") - values.shift() - values.push(random) - - updatingChart - .text(values.join(",")) - .change() - }, 1000) + if (updatingChart.length > 0) { + setInterval(function() { + var random = Math.round(Math.random() * 10) + var values = updatingChart.text().split(",") + values.shift() + values.push(random) + + updatingChart + .text(values.join(",")) + .change() + }, 1000) + } })