Uncaught TypeError: X is not a function. While using javascript closure
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?
javascript closures settimeout infinite-loop
add a comment |
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?
javascript closures settimeout infinite-loop
1
createDiv(10)
invokes the function, and it will returnvoid
as your have no return statement in the function. Thereforediv
is not a function butundefined
. Did you try console loggingdiv?
after it was set?
– pmkro
Nov 20 '18 at 21:56
Thanks. Yes in console div isundefined
. How it fix?
– Seva
Nov 20 '18 at 22:01
What do you think its supposed to be? As soon as you usecreateDiv
it will run thesetTimeout
. Look at answer response.
– pmkro
Nov 20 '18 at 22:03
What isdiv()
supposed to do?
– Barmar
Nov 20 '18 at 22:07
add a comment |
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?
javascript closures settimeout infinite-loop
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
javascript closures settimeout infinite-loop
edited Nov 20 '18 at 21:59
ggorlen
7,3883826
7,3883826
asked Nov 20 '18 at 21:51
SevaSeva
3461422
3461422
1
createDiv(10)
invokes the function, and it will returnvoid
as your have no return statement in the function. Thereforediv
is not a function butundefined
. Did you try console loggingdiv?
after it was set?
– pmkro
Nov 20 '18 at 21:56
Thanks. Yes in console div isundefined
. How it fix?
– Seva
Nov 20 '18 at 22:01
What do you think its supposed to be? As soon as you usecreateDiv
it will run thesetTimeout
. Look at answer response.
– pmkro
Nov 20 '18 at 22:03
What isdiv()
supposed to do?
– Barmar
Nov 20 '18 at 22:07
add a comment |
1
createDiv(10)
invokes the function, and it will returnvoid
as your have no return statement in the function. Thereforediv
is not a function butundefined
. Did you try console loggingdiv?
after it was set?
– pmkro
Nov 20 '18 at 21:56
Thanks. Yes in console div isundefined
. How it fix?
– Seva
Nov 20 '18 at 22:01
What do you think its supposed to be? As soon as you usecreateDiv
it will run thesetTimeout
. Look at answer response.
– pmkro
Nov 20 '18 at 22:03
What isdiv()
supposed to do?
– Barmar
Nov 20 '18 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 '18 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 '18 at 21:56
Thanks. Yes in console div is
undefined
. How it fix?– Seva
Nov 20 '18 at 22:01
Thanks. Yes in console div is
undefined
. How it fix?– Seva
Nov 20 '18 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 '18 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 '18 at 22:03
What is
div()
supposed to do?– Barmar
Nov 20 '18 at 22:07
What is
div()
supposed to do?– Barmar
Nov 20 '18 at 22:07
add a comment |
2 Answers
2
active
oldest
votes
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);
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 20 '18 at 22:10
ggorlenggorlen
7,3883826
7,3883826
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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')
}
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')
}
edited Nov 20 '18 at 22:05
answered Nov 20 '18 at 21:59
pmkropmkro
1,465717
1,465717
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
createDiv(10)
invokes the function, and it will returnvoid
as your have no return statement in the function. Thereforediv
is not a function butundefined
. Did you try console loggingdiv?
after it was set?– pmkro
Nov 20 '18 at 21:56
Thanks. Yes in console div is
undefined
. How it fix?– Seva
Nov 20 '18 at 22:01
What do you think its supposed to be? As soon as you use
createDiv
it will run thesetTimeout
. Look at answer response.– pmkro
Nov 20 '18 at 22:03
What is
div()
supposed to do?– Barmar
Nov 20 '18 at 22:07