I'm a newbie developing a small web application that uses HTML, JavaScript (Angular 1.5.0) & CSS. I am using Grunt to process, jslint, minify my javascript and CSS files. My Javascript code includes an SVG image created by d3.js.
I'm currently getting the following warning from jshint:
line 1932 col 32 Don't make functions within a loop.
How can I rewrite the code below to eliminate this warning?
Line #1924 for (var j = 0; j <= 5; j++) {
Line #1925 var myVarX = j * 100;
Line #1926 var myVarY = j * 200;
Line #1927 var myText = self.svgContainer.append("text")
Line #1928 .attr("x", myVarX)
Line #1929 .attr("y", myVarY)
Line #1930 .text("Hello World")
Line #1931 .attr("fill", "black")
Line #1932 .attr("transform", function(d){
Line #1933 var bb = this.getBBox();
Line #1934 leftBoundary = bb.x + (bb.width / (-2));
Line #1935 return "translate(" + (bb.width / (-2)) + ", 0)";
Line #1936 }
Line #1937 )
Line #1938 if (leftBoundary > centerX + 5)
Line #1939 myText.style("display", "block");
Line #1940 else
Line #1941 myText.remove();
Line #1942 }
Move this
function(d){
var bb = this.getBBox();
leftBoundary = bb.x + (bb.width / (-2));
return "translate(" + (bb.width / (-2)) + ", 0)";
}
outside the scope of the for loop. Like:
var transformCallback = function(d) {
var bb = this.getBBox();
...
and then use transformCallback
in place of the function.
.attr("transform", transformCallback)