如何使用HTML / JS循环更改文本

时间:2018-05-23 19:09:04

标签: javascript html

这是我发现的代码:

library(tidyverse)
library(Lahman)

#Brings salary information together with batting
bat_salaries <- left_join(Batting,Salaries, suffix = c(".x", ".y"))
bat_salaries <- left_join(bat_salaries, Teams, by = c("yearID", "teamID", "lgID"), suffix = c("_individual", "_team"))

#I noticed the tail of bat_salaries$salary is very heavy after the 3rd IQR - I cut it off to only look at
#data before the 3rd IQR

bat_salaries_iqr3 <- bat_salaries %>%
  filter(salary < 2350000 & salary > 0)

bat_salaries_chi <- bat_salaries_iqr3 %>%
  select(salary) %>%
  mutate(leagID = ifelse(bat_salaries_iqr3$lgID == "NL", 1, 0))

chisq.test(table(bat_salaries_chi), correct = FALSE)


Pearson's Chi-squared test

data:  table(bat_salaries_chi)
X-squared = 2462.6, df = 2139, p-value = 1.13e-06

显示序列的div

<script>
    var example = ['A', 'B', 'C', 'D'];

    textSequence(0);
    function textSequence(i) {

        if (example.length > i) {
            setTimeout(function() {
                document.getElementById("sequence").innerHTML = example[i];
                textSequence(++i);
            }, 3000); // 3 seconds (in milliseconds)

        } else if (example.length == i) { // Loop
            textSequence(0);
        }

    }
</script>

我想改变的是:

  1. 在页面加载时立即显示第一个文本而不是3秒钟。
  2. 要进行幻灯片放入/滑出效果转换
  3. 我该怎么做?

1 个答案:

答案 0 :(得分:0)

我稍微更改了你的代码,所以它会马上开始。 对于幻灯片的进出,你最好使用jQuery而不是纯Javascript。

<script>
    var example = ['A', 'B', 'C', 'D'];
    var gFirstTime = true;
    textSequence(0);


    function textSequence(i) {
        if (gFirstTime) {
            timeToWait = 0;
            gFirstTime = false;
        }
        else {
            timeToWait = 3000;
        }
        if (example.length > i) {
            setTimeout(function () {
                var sequenceDiv = document.getElementById("sequence");                                                          
                sequenceDiv.innerHTML = example[i];                    
                textSequence(++i);
            }, timeToWait); // 3 seconds (in milliseconds)

        } else if (example.length == i) { // Loop
            textSequence(0);
        }

    }
</script>