2016-11-04 9 views
1

JavaScriptを使用してインターバルタイマーを設定しようとすると、イメージを配列に配置する方法がわかりません。
私はいくつかの異なる方法を試しましたが、動作させることはできません。x秒ごとに新しい画像を表示するには?

var myImage = document.getElementById("mainImage"); 

var imageArray = [img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415", 
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415", 
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415"] 

var imageIndex = 0; 

function changeImage() { 
    myImage.setAttribute("src",imageArray[imageIndex]); 
    imageIndex++; 
    if(imageIndex >= imageArray.length) { 
    indexImage = 0; 
    } 
} 

setInterval(changeImage,5000); 

事前に感謝しています。また、私は新しく、答えを探してみましたが、見つからなかった。あなたの配列は、文字列の配列である必要があり

+1

あなた 'imageArray'は有効な配列ではありません。基本的に '['と ']'の間には巨大な構文エラーがあります。あなたのコードに基づいて、配列内にソースを置くだけでよい: 'var imageArray = [" http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg "、" .. 。 "、" ... "];' –

+0

そして 'imageIndex!== indexImage' – Andreas

答えて

2

いくつか

  1. あなたがソース

  2. 以上で構成され、文字列全体にsrc属性を設定しています

    indexImageが定義されていません

    var myImage = document.getElementById("mainImage"); 
    
    var imageArray = [ 
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg", 
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg", 
        "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" 
    ] 
    
    var imageIndex = 0; 
    
    function changeImage() { 
        myImage.setAttribute("src", imageArray[imageIndex]); 
        imageIndex++; 
        if (imageIndex >= imageArray.length) { 
         imageIndex = 0; 
        } 
    } 
    
    setInterval(changeImage, 5000); 
    
+1

imageArrayの定義 – Corporalis

+0

の下にある'} 'を閉じる'] 'に置き換えてください。それをキャッチするためにありがとう! –

0

まず、否定的なコメントではありませんが、あなたが望むものを配列に投げることはできません。他のすべての変数が静的なイメージを作成するには、最初のと同じであるため、彼らは...

  • を文字列、数値、FUNC、OBJを保持することができます

<img id="mainImage" class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" onload="google.aft&amp;&amp;google.aft(this)" width="626" height="415"/>

function changeImage() { 
 
     myImage.setAttribute("src",imageArray[imageIndex]); 
 
     imageIndex++; 
 
     if(imageIndex >= imageArray.length) { 
 
     indexImage = 0; 
 
     } 
 
    }

+0

すべての助けてくれてありがとう、私はそれを否定的に受け止めなかった。私はどのようにイメージを配列に入れるのかよく分かりませんでした。私はまだカント私のイメージを表示するが、タイマーが動作します。それは私にaltを与えるので私はまだ何かが欠けている。 – chadmlusby

+0

もう一度感謝しました。 – chadmlusby

関連する問題