+ 1
Uncaught error: cannot set property 'src' of null
what's the problem in 'src'?
4 ответов
+ 5
Variable randomImg is wrong.
Because math.random generate any type of no e.g. 4 and array length is 4, then the execution will be 4*4=16 and 16th val or 15th array val(array start with 0) is not present so it cause an null value as error.
+ 4
Share code
0
var myImge = document.getElementById('myImg'),
arrImgs = [
"https://placehold.it/100/#fff",
"https://placehold.it/100/#f00",
"https://placehold.it/100/#00f",
"https://placehold.it/100/#ddd"
];
function changeImg(myImge, arrImgs) {
'use strict';
setInterval(function () {
var randomImg = Math.floor(Math.random() * arrImgs.length);
myImge.src = arrImgs[randomImg];
}, 1000);
};
window.onload = changeImg(myImge, arrImgs);
0
I checked that Code in Netbeans many times ,and it's totaly Correct..but in Sololearn ..I don't what's up here..