setTimeout()只工作2次

时间:2016-11-29 20:30:16

标签: javascript

这是我的代码

<script type="text/javascript">
window.onload=change;
var i=0 ;
function change()
{
    var images = new Array('1','2','3','4','5','6');
    document.getElementById('baner').src = obrazy[i];
    i++
    if (i == images.length)
    {
        i=0;
    }
}
setTimeout(change,1000);
</script>

我想制作简单的滑块但是setTimeout只做了2次我的脚本。是的,我有id baner的元素。我不知道那有什么问题。

2 个答案:

答案 0 :(得分:4)

您只在change处理window.onload两次,一次在setInterval的处理程序中,一次在超时1秒后。

您可能正在寻找setTimeout 而不是window.onload = function() { var i=0 ; function change() { var images = new Array('1','2','3','4','5','6'); document.getElementById('baner').src = obrazy[i]; i++; if (i == images.length) { i=0; } } setInterval(change,1000); change(); }

{
"type": "manifest",
"hasThumbnail": "false",
"status": "failed",
"progress": "complete",
"region": "US",
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6d2VndGVzdGJ1Y2tldC9Pd2luZ3NNaWxscy5ydnQ",
"version": "1.0",
"derivatives": [
    {
        "name": "OwingsMills.rvt",
        "hasThumbnail": "false",
        "status": "failed",
        "progress": "complete",
        "messages": [
            {
                "type": "error",
                "code": "Revit-UnsupportedFileType",
                "message": "<message>The file is not a Revit file or is not a supported version.</message>"
            },
            {
                "type": "error",
                "message": "Possibly recoverable warning exit code from extractor: -536870935",
                "code": "TranslationWorker-RecoverableInternalFailure"
            }
        ],
        "outputType": "svf"
    }
]

答案 1 :(得分:1)

您应该使用调用函数的setInterval()方法或以指定的时间间隔(以毫秒为单位)计算表达式。

<script type="text/javascript">
window.onload=change;
var i=0 ;
function change()
{
    var images = new Array('1','2','3','4','5','6');
    document.getElementById('baner').src = obrazy[i];
    i++
    if (i == images.length)
    {
        i=0;
    }
}
setInterval(change,1000);
</script>

setTimeout()方法在指定的毫秒数后调用函数并停止。