Still learning JS since I'm a noob so bear with me.
I have a web app with 2 circular SVG gauges that currently work and I receive this issue before users log-in.
My Problem: I get
"Uncaught TypeError: Cannot read property 'setAttribute' of null" firing off like crazy
for pathElementTwo.setAttribute('d', describeArc(26, 0, arcTwo)); in console because the area where that specific arc only needs to load when users log in. This only fires off like crazy in console before login, after login it disappears.
How can I fix this issue so console doesn't fire off like crazy before users log in?
Any help is gladly appreciated. Thanks!
JS
function describeArc(radius, startAngle, endAngle) {
// Helper function, used to convert the (startAngle, endAngle) arc
// dexcription into cartesian coordinates that are used for the
// SVG arc descriptor.
function polarToCartesian(radius, angle) {
return {
x: radius * Math.cos(angle),
y: radius * Math.sin(angle),
};
}
// Generate cartesian coordinates for the start and end of the arc.
var start = polarToCartesian(radius, endAngle);
var end = polarToCartesian(radius, startAngle);
// Determine if we're drawing an arc that's larger than a 1/2 circle.
var largeArcFlag = endAngle - startAngle <= Math.PI ? 0 : 1;
// Generate the SVG arc descriptor.
var d = [
'M', start.x, start.y,
'A', radius, radius, 1, largeArcFlag, 0, end.x, end.y
].join(' ');
return d;
}
var arc = 0;
var arcTwo = 0;
setInterval(function() {
// Update the ticker progress.
arc += Math.PI / 1000;
arcTwo += Math.PI / 1000;
if (arc >= 2 * Math.PI) { arc = 0; }
if (arcTwo >= 2 * Math.PI) { arcTwo = 0; }
// Update the SVG arc descriptor.
var pathElement = document.getElementById('arc-path');
var pathElementTwo = document.getElementById('arc-path-two');
pathElement.setAttribute('d', describeArc(26, 0, arc));
pathElementTwo.setAttribute('d', describeArc(26, 0, arcTwo));
}, 400 / 0)
HTML
<div class="ticker-body">
<svg viewBox="19, -19 65 35" class="gauge-background"
fill="none">
<circle r="10"/>
</svg>
<svg viewBox="-39, -39 700 75" class="gauge" fill="none">
<path id="arc-path" transform="rotate(-90)" stroke-
linecap="circle" />
</svg>
</div>
<div class="overlay-collect"></div>
<div class="hot-offer-btn"></div>
<div class="ticker-body-two">
<svg viewBox="4, -19 65 35" class="gauge-background-
two" fill="none">
<circle r="10"/>
</svg>
<svg viewBox="-51, -34 450 75" class="gauge-two"
fill="none">
<path id="arc-path-two" transform="rotate(-90)"
stroke-linecap="circle" />
</svg>
</div>
getElementByIdis not returning the object. Could you post your html? And that second argument onsetIntervalprobably shoudn't be a division by 0pathElementTwodoesn't exist yet. Are you waiting till the DOM is ready? If it is an external SVG, are you waiting for it to load before trying to access its DOM?