Uncaught TypeError: X is not a function. While using javascript closure












0














I'm playing with JavaScript and see following error in console:




Uncaught TypeError: div is not a function.







function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();





I left the following for simplicity:



function createDiv(interval) {
let timerId = window.setTimeout(function tick()
{
setTimeout(tick, 10);
}, 10)
}

let div = createDiv(10);
div();


This is essentially an infinite loop. And error occurs when calling div() but loop is working.



What is wrong? And how it fix?










share|improve this question




















  • 1




    createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
    – pmkro
    Nov 20 at 21:56










  • Thanks. Yes in console div is undefined. How it fix?
    – Seva
    Nov 20 at 22:01










  • What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
    – pmkro
    Nov 20 at 22:03










  • What is div() supposed to do?
    – Barmar
    Nov 20 at 22:07
















0














I'm playing with JavaScript and see following error in console:




Uncaught TypeError: div is not a function.







function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();





I left the following for simplicity:



function createDiv(interval) {
let timerId = window.setTimeout(function tick()
{
setTimeout(tick, 10);
}, 10)
}

let div = createDiv(10);
div();


This is essentially an infinite loop. And error occurs when calling div() but loop is working.



What is wrong? And how it fix?










share|improve this question




















  • 1




    createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
    – pmkro
    Nov 20 at 21:56










  • Thanks. Yes in console div is undefined. How it fix?
    – Seva
    Nov 20 at 22:01










  • What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
    – pmkro
    Nov 20 at 22:03










  • What is div() supposed to do?
    – Barmar
    Nov 20 at 22:07














0












0








0







I'm playing with JavaScript and see following error in console:




Uncaught TypeError: div is not a function.







function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();





I left the following for simplicity:



function createDiv(interval) {
let timerId = window.setTimeout(function tick()
{
setTimeout(tick, 10);
}, 10)
}

let div = createDiv(10);
div();


This is essentially an infinite loop. And error occurs when calling div() but loop is working.



What is wrong? And how it fix?










share|improve this question















I'm playing with JavaScript and see following error in console:




Uncaught TypeError: div is not a function.







function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();





I left the following for simplicity:



function createDiv(interval) {
let timerId = window.setTimeout(function tick()
{
setTimeout(tick, 10);
}, 10)
}

let div = createDiv(10);
div();


This is essentially an infinite loop. And error occurs when calling div() but loop is working.



What is wrong? And how it fix?






function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();





function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick()
{
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.left = (radius + radius * Math.cos(f)) + 'px';
div.style.top = (radius + radius * Math.sin(f)) + 'px';
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)

}

let div = createDiv(10);
div();






javascript closures settimeout infinite-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:59









ggorlen

6,3963825




6,3963825










asked Nov 20 at 21:51









Seva

3111422




3111422








  • 1




    createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
    – pmkro
    Nov 20 at 21:56










  • Thanks. Yes in console div is undefined. How it fix?
    – Seva
    Nov 20 at 22:01










  • What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
    – pmkro
    Nov 20 at 22:03










  • What is div() supposed to do?
    – Barmar
    Nov 20 at 22:07














  • 1




    createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
    – pmkro
    Nov 20 at 21:56










  • Thanks. Yes in console div is undefined. How it fix?
    – Seva
    Nov 20 at 22:01










  • What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
    – pmkro
    Nov 20 at 22:03










  • What is div() supposed to do?
    – Barmar
    Nov 20 at 22:07








1




1




createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
– pmkro
Nov 20 at 21:56




createDiv(10) invokes the function, and it will return void as your have no return statement in the function. Therefore div is not a function but undefined. Did you try console logging div? after it was set?
– pmkro
Nov 20 at 21:56












Thanks. Yes in console div is undefined. How it fix?
– Seva
Nov 20 at 22:01




Thanks. Yes in console div is undefined. How it fix?
– Seva
Nov 20 at 22:01












What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
– pmkro
Nov 20 at 22:03




What do you think its supposed to be? As soon as you use createDiv it will run the setTimeout. Look at answer response.
– pmkro
Nov 20 at 22:03












What is div() supposed to do?
– Barmar
Nov 20 at 22:07




What is div() supposed to do?
– Barmar
Nov 20 at 22:07












2 Answers
2






active

oldest

votes


















2














There is no closure here: you're calling a function that returns undefined and attempting to call undefined() as if it were a function. Simply call your function like createDiv(10);. Note that I've added a height and width to your div so the result is visible.






function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function createDiv(interval) {
let radius = 300;
let int = interval;
var f = 0;
var s = 2 * Math.PI / 180;
let timerId = window.setTimeout(function tick() {
var div = document.createElement('div');
div.className = 'circle';
div.style.backgroundColor = getRandomColor();
div.style.height = div.style.width = "50px";
document.body.appendChild(div);
f += s;
setTimeout(tick, int);
}, int)
}

createDiv(1000);








share|improve this answer





























    1

















    function getRandomColor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
    }

    function createDiv(interval) {
    let radius = 300;
    let int = interval;
    var f = 0;
    var s = 2 * Math.PI / 180;
    window.setTimeout(function tick()
    {
    var div = document.createElement('div');
    div.className = 'circle';
    div.style.backgroundColor = getRandomColor();
    div.style.left = (radius + radius * Math.cos(f)) + 'px';
    div.style.top = (radius + radius * Math.sin(f)) + 'px';
    document.body.appendChild(div);
    f += s;
    setTimeout(tick, int);
    }, int)

    }

    let div = createDiv(10);
    console.log(div);
    try {
    div();
    } catch (err) {
    console.log('error')
    }


    function createDivSmall(interval) {
    window.setTimeout(function tick()
    {
    setTimeout(tick, interval);
    }, 10)
    }

    let divSmall = createDivSmall(10);
    console.log(divSmall);
    try {
    divSmall();
    } catch (err) {
    console.log('Small error')
    }





    In your createDiv function you are not returning anything, so the default behavior is to return undefined. This sets div = undefined. At this point div is not a function so trying to invoke it will throw an error. createDiv is starting the loop for you and running it, there is not need to set a variable to the return value.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402127%2funcaught-typeerror-x-is-not-a-function-while-using-javascript-closure%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      There is no closure here: you're calling a function that returns undefined and attempting to call undefined() as if it were a function. Simply call your function like createDiv(10);. Note that I've added a height and width to your div so the result is visible.






      function getRandomColor() {
      var letters = '0123456789ABCDEF';
      var color = '#';
      for (var i = 0; i < 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
      }

      function createDiv(interval) {
      let radius = 300;
      let int = interval;
      var f = 0;
      var s = 2 * Math.PI / 180;
      let timerId = window.setTimeout(function tick() {
      var div = document.createElement('div');
      div.className = 'circle';
      div.style.backgroundColor = getRandomColor();
      div.style.height = div.style.width = "50px";
      document.body.appendChild(div);
      f += s;
      setTimeout(tick, int);
      }, int)
      }

      createDiv(1000);








      share|improve this answer


























        2














        There is no closure here: you're calling a function that returns undefined and attempting to call undefined() as if it were a function. Simply call your function like createDiv(10);. Note that I've added a height and width to your div so the result is visible.






        function getRandomColor() {
        var letters = '0123456789ABCDEF';
        var color = '#';
        for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
        }

        function createDiv(interval) {
        let radius = 300;
        let int = interval;
        var f = 0;
        var s = 2 * Math.PI / 180;
        let timerId = window.setTimeout(function tick() {
        var div = document.createElement('div');
        div.className = 'circle';
        div.style.backgroundColor = getRandomColor();
        div.style.height = div.style.width = "50px";
        document.body.appendChild(div);
        f += s;
        setTimeout(tick, int);
        }, int)
        }

        createDiv(1000);








        share|improve this answer
























          2












          2








          2






          There is no closure here: you're calling a function that returns undefined and attempting to call undefined() as if it were a function. Simply call your function like createDiv(10);. Note that I've added a height and width to your div so the result is visible.






          function getRandomColor() {
          var letters = '0123456789ABCDEF';
          var color = '#';
          for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
          }
          return color;
          }

          function createDiv(interval) {
          let radius = 300;
          let int = interval;
          var f = 0;
          var s = 2 * Math.PI / 180;
          let timerId = window.setTimeout(function tick() {
          var div = document.createElement('div');
          div.className = 'circle';
          div.style.backgroundColor = getRandomColor();
          div.style.height = div.style.width = "50px";
          document.body.appendChild(div);
          f += s;
          setTimeout(tick, int);
          }, int)
          }

          createDiv(1000);








          share|improve this answer












          There is no closure here: you're calling a function that returns undefined and attempting to call undefined() as if it were a function. Simply call your function like createDiv(10);. Note that I've added a height and width to your div so the result is visible.






          function getRandomColor() {
          var letters = '0123456789ABCDEF';
          var color = '#';
          for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
          }
          return color;
          }

          function createDiv(interval) {
          let radius = 300;
          let int = interval;
          var f = 0;
          var s = 2 * Math.PI / 180;
          let timerId = window.setTimeout(function tick() {
          var div = document.createElement('div');
          div.className = 'circle';
          div.style.backgroundColor = getRandomColor();
          div.style.height = div.style.width = "50px";
          document.body.appendChild(div);
          f += s;
          setTimeout(tick, int);
          }, int)
          }

          createDiv(1000);








          function getRandomColor() {
          var letters = '0123456789ABCDEF';
          var color = '#';
          for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
          }
          return color;
          }

          function createDiv(interval) {
          let radius = 300;
          let int = interval;
          var f = 0;
          var s = 2 * Math.PI / 180;
          let timerId = window.setTimeout(function tick() {
          var div = document.createElement('div');
          div.className = 'circle';
          div.style.backgroundColor = getRandomColor();
          div.style.height = div.style.width = "50px";
          document.body.appendChild(div);
          f += s;
          setTimeout(tick, int);
          }, int)
          }

          createDiv(1000);





          function getRandomColor() {
          var letters = '0123456789ABCDEF';
          var color = '#';
          for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
          }
          return color;
          }

          function createDiv(interval) {
          let radius = 300;
          let int = interval;
          var f = 0;
          var s = 2 * Math.PI / 180;
          let timerId = window.setTimeout(function tick() {
          var div = document.createElement('div');
          div.className = 'circle';
          div.style.backgroundColor = getRandomColor();
          div.style.height = div.style.width = "50px";
          document.body.appendChild(div);
          f += s;
          setTimeout(tick, int);
          }, int)
          }

          createDiv(1000);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 22:10









          ggorlen

          6,3963825




          6,3963825

























              1

















              function getRandomColor() {
              var letters = '0123456789ABCDEF';
              var color = '#';
              for (var i = 0; i < 6; i++) {
              color += letters[Math.floor(Math.random() * 16)];
              }
              return color;
              }

              function createDiv(interval) {
              let radius = 300;
              let int = interval;
              var f = 0;
              var s = 2 * Math.PI / 180;
              window.setTimeout(function tick()
              {
              var div = document.createElement('div');
              div.className = 'circle';
              div.style.backgroundColor = getRandomColor();
              div.style.left = (radius + radius * Math.cos(f)) + 'px';
              div.style.top = (radius + radius * Math.sin(f)) + 'px';
              document.body.appendChild(div);
              f += s;
              setTimeout(tick, int);
              }, int)

              }

              let div = createDiv(10);
              console.log(div);
              try {
              div();
              } catch (err) {
              console.log('error')
              }


              function createDivSmall(interval) {
              window.setTimeout(function tick()
              {
              setTimeout(tick, interval);
              }, 10)
              }

              let divSmall = createDivSmall(10);
              console.log(divSmall);
              try {
              divSmall();
              } catch (err) {
              console.log('Small error')
              }





              In your createDiv function you are not returning anything, so the default behavior is to return undefined. This sets div = undefined. At this point div is not a function so trying to invoke it will throw an error. createDiv is starting the loop for you and running it, there is not need to set a variable to the return value.






              share|improve this answer




























                1

















                function getRandomColor() {
                var letters = '0123456789ABCDEF';
                var color = '#';
                for (var i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
                }
                return color;
                }

                function createDiv(interval) {
                let radius = 300;
                let int = interval;
                var f = 0;
                var s = 2 * Math.PI / 180;
                window.setTimeout(function tick()
                {
                var div = document.createElement('div');
                div.className = 'circle';
                div.style.backgroundColor = getRandomColor();
                div.style.left = (radius + radius * Math.cos(f)) + 'px';
                div.style.top = (radius + radius * Math.sin(f)) + 'px';
                document.body.appendChild(div);
                f += s;
                setTimeout(tick, int);
                }, int)

                }

                let div = createDiv(10);
                console.log(div);
                try {
                div();
                } catch (err) {
                console.log('error')
                }


                function createDivSmall(interval) {
                window.setTimeout(function tick()
                {
                setTimeout(tick, interval);
                }, 10)
                }

                let divSmall = createDivSmall(10);
                console.log(divSmall);
                try {
                divSmall();
                } catch (err) {
                console.log('Small error')
                }





                In your createDiv function you are not returning anything, so the default behavior is to return undefined. This sets div = undefined. At this point div is not a function so trying to invoke it will throw an error. createDiv is starting the loop for you and running it, there is not need to set a variable to the return value.






                share|improve this answer


























                  1












                  1








                  1









                  function getRandomColor() {
                  var letters = '0123456789ABCDEF';
                  var color = '#';
                  for (var i = 0; i < 6; i++) {
                  color += letters[Math.floor(Math.random() * 16)];
                  }
                  return color;
                  }

                  function createDiv(interval) {
                  let radius = 300;
                  let int = interval;
                  var f = 0;
                  var s = 2 * Math.PI / 180;
                  window.setTimeout(function tick()
                  {
                  var div = document.createElement('div');
                  div.className = 'circle';
                  div.style.backgroundColor = getRandomColor();
                  div.style.left = (radius + radius * Math.cos(f)) + 'px';
                  div.style.top = (radius + radius * Math.sin(f)) + 'px';
                  document.body.appendChild(div);
                  f += s;
                  setTimeout(tick, int);
                  }, int)

                  }

                  let div = createDiv(10);
                  console.log(div);
                  try {
                  div();
                  } catch (err) {
                  console.log('error')
                  }


                  function createDivSmall(interval) {
                  window.setTimeout(function tick()
                  {
                  setTimeout(tick, interval);
                  }, 10)
                  }

                  let divSmall = createDivSmall(10);
                  console.log(divSmall);
                  try {
                  divSmall();
                  } catch (err) {
                  console.log('Small error')
                  }





                  In your createDiv function you are not returning anything, so the default behavior is to return undefined. This sets div = undefined. At this point div is not a function so trying to invoke it will throw an error. createDiv is starting the loop for you and running it, there is not need to set a variable to the return value.






                  share|improve this answer

















                  function getRandomColor() {
                  var letters = '0123456789ABCDEF';
                  var color = '#';
                  for (var i = 0; i < 6; i++) {
                  color += letters[Math.floor(Math.random() * 16)];
                  }
                  return color;
                  }

                  function createDiv(interval) {
                  let radius = 300;
                  let int = interval;
                  var f = 0;
                  var s = 2 * Math.PI / 180;
                  window.setTimeout(function tick()
                  {
                  var div = document.createElement('div');
                  div.className = 'circle';
                  div.style.backgroundColor = getRandomColor();
                  div.style.left = (radius + radius * Math.cos(f)) + 'px';
                  div.style.top = (radius + radius * Math.sin(f)) + 'px';
                  document.body.appendChild(div);
                  f += s;
                  setTimeout(tick, int);
                  }, int)

                  }

                  let div = createDiv(10);
                  console.log(div);
                  try {
                  div();
                  } catch (err) {
                  console.log('error')
                  }


                  function createDivSmall(interval) {
                  window.setTimeout(function tick()
                  {
                  setTimeout(tick, interval);
                  }, 10)
                  }

                  let divSmall = createDivSmall(10);
                  console.log(divSmall);
                  try {
                  divSmall();
                  } catch (err) {
                  console.log('Small error')
                  }





                  In your createDiv function you are not returning anything, so the default behavior is to return undefined. This sets div = undefined. At this point div is not a function so trying to invoke it will throw an error. createDiv is starting the loop for you and running it, there is not need to set a variable to the return value.






                  function getRandomColor() {
                  var letters = '0123456789ABCDEF';
                  var color = '#';
                  for (var i = 0; i < 6; i++) {
                  color += letters[Math.floor(Math.random() * 16)];
                  }
                  return color;
                  }

                  function createDiv(interval) {
                  let radius = 300;
                  let int = interval;
                  var f = 0;
                  var s = 2 * Math.PI / 180;
                  window.setTimeout(function tick()
                  {
                  var div = document.createElement('div');
                  div.className = 'circle';
                  div.style.backgroundColor = getRandomColor();
                  div.style.left = (radius + radius * Math.cos(f)) + 'px';
                  div.style.top = (radius + radius * Math.sin(f)) + 'px';
                  document.body.appendChild(div);
                  f += s;
                  setTimeout(tick, int);
                  }, int)

                  }

                  let div = createDiv(10);
                  console.log(div);
                  try {
                  div();
                  } catch (err) {
                  console.log('error')
                  }


                  function createDivSmall(interval) {
                  window.setTimeout(function tick()
                  {
                  setTimeout(tick, interval);
                  }, 10)
                  }

                  let divSmall = createDivSmall(10);
                  console.log(divSmall);
                  try {
                  divSmall();
                  } catch (err) {
                  console.log('Small error')
                  }





                  function getRandomColor() {
                  var letters = '0123456789ABCDEF';
                  var color = '#';
                  for (var i = 0; i < 6; i++) {
                  color += letters[Math.floor(Math.random() * 16)];
                  }
                  return color;
                  }

                  function createDiv(interval) {
                  let radius = 300;
                  let int = interval;
                  var f = 0;
                  var s = 2 * Math.PI / 180;
                  window.setTimeout(function tick()
                  {
                  var div = document.createElement('div');
                  div.className = 'circle';
                  div.style.backgroundColor = getRandomColor();
                  div.style.left = (radius + radius * Math.cos(f)) + 'px';
                  div.style.top = (radius + radius * Math.sin(f)) + 'px';
                  document.body.appendChild(div);
                  f += s;
                  setTimeout(tick, int);
                  }, int)

                  }

                  let div = createDiv(10);
                  console.log(div);
                  try {
                  div();
                  } catch (err) {
                  console.log('error')
                  }


                  function createDivSmall(interval) {
                  window.setTimeout(function tick()
                  {
                  setTimeout(tick, interval);
                  }, 10)
                  }

                  let divSmall = createDivSmall(10);
                  console.log(divSmall);
                  try {
                  divSmall();
                  } catch (err) {
                  console.log('Small error')
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 22:05

























                  answered Nov 20 at 21:59









                  pmkro

                  1,354617




                  1,354617






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402127%2funcaught-typeerror-x-is-not-a-function-while-using-javascript-closure%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen