Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   PHP, PERL, MySQL, JavaScript (https://forum.antichat.xyz/forumdisplay.php?f=37)
-   -   Изображения и JavaScript (https://forum.antichat.xyz/showthread.php?t=194675)

-slim- 08.04.2010 21:27

Изображения и JavaScript
 
Помогите сообразить почему не работает код.

Код:

<html>
<head>
<title>...</title>
<script type="text/javascript">
function ImgSwitch(id, first, last, time) {
    var img = document.getElementById(id);
    if (!img) return;
    if (img._T) clearInterval(img._T);
    img._T = setInterval(function(){
        var i = Math.floor(Math.random() * (last-first+1) + first);
        img.src = 'img/file_' + i + '.jpg';
    }, time);
};
ImgSwitch.Stop = function(id) {
    var img = document.getElementById(id);
    if (img && img._T) {
        clearInterval(img._T);
        img._T = void 0;
    };
};
 
</script>
</head>

<body>

<script>
ImgSwitch('foobar', 1, 10, 5000); // случайные от 1 до 10 каждые 5 сек.
 

ImgSwitch.Stop('foobar');
</script>

<img src="img/file_1.jpg" id="foobar" />

</body>
</html>


cha0t1k 09.04.2010 11:37

Браузер отрабатывает код по мере загрузки. У тебя джаваскрипт отработает до того как загрузится сам тег. Помести код джаваскрипта ниже тега <img /> :)
А лучше помести вызовы функций в событие window.onload


Время: 18:01