本文介绍了如何使用 setInterval() 更改文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了一段时间.我正在尝试创建一个简单的倒计时计时器.我已经让 alert() 工作了,但不幸的是 textToChange 没有改变.我在下面复制了我的代码的副本.任何帮助将不胜感激!谢谢.

I've been working at this for quite some time now. I'm trying to create a simple timer that counts down. I've gotten the alert() to work, but unfortunately the textToChange does not change. I've reproduced a copy of my code below. Any help would be greatly appreciated! Thank you.

<script type="text/javascript">
    var timeLeft = 0;
    var changingTextElement;
    var changingText = new Array();
    var ctr = 0;

    function timeMsg(thing)
    {
    var length = thing.inputbox.value*1000;
    var t = setTimeout("alertMsg()",length);
    timeLeft = length/1000;
    initChangeText();
    }

    function alertMsg()
    {
    alert("Alert!");
    }

    function initChangeText(){
        changingTextElement = document.getElementById("textToChange");
        changingText[ctr] = changingTextElement.innerHTML;
        ctr++;
        while(timeLeft > 0){
            changingText[ctr] = timeLeft;
            timeLeft--;
            ctr++;
        }
        ctr = 0;
        setInterval("changingText()",1000);
    }

    function changingText() {
        ctr++;
        if(ctr >= changingText.length){
            changingTextElement.innerHTML = 0;
        }
        else{
            changingTextElement.innerHTML = changingText[ctr];
        }
    }

</script>

推荐答案

您正在使用 changedText 函数和 keepingText 数组 ...

You are using a changingText function and a changingText array ...

看到冲突了吗?

作为附加信息,

不要在setInterval 方法中使用字符串作为名称函数.直接引用方法

do not use strings as name functions in the setInterval method. Use a reference to the method directly

setInterval(changingText, 1000);

这篇关于如何使用 setInterval() 更改文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:03