+ 1

What' s wrong in this code?

The error is "cannot change property style of null". I can' t understand why it doesn' t work... var l = setInterval(1, o()); var x = 500; function o() { x-- var g = document.getElementById('p'); g.style.height = x + 'px'; if (x <= 100) { clearInterval(l) } }

14th Feb 2021, 12:15 AM
Gabriele Giambrone
Gabriele Giambrone - avatar
3 odpowiedzi
+ 4
Wrong argument and wrong sequence of arguments passed into setInterval setInterval accepts two arguments The second argument is time in millisecond. The first argument is a callback function. When you pass a function as callback, just put the function name. Don't add parenthesis, because parenthesis will execute it and you will be passing the executed result instead.
14th Feb 2021, 1:19 AM
Gordon
Gordon - avatar
+ 4
var x = 500; function o() { x-- var g = document.getElementById('p'); g.style.height = x + 'px'; if (x <= 100) { clearInterval(l) } } var l = setInterval(o,1); try this one.
14th Feb 2021, 1:00 AM
Labib Hasan
Labib Hasan - avatar
+ 1
Thanks Labib Hasan and Gordon, I'll try it
14th Feb 2021, 10:50 AM
Gabriele Giambrone
Gabriele Giambrone - avatar