Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a jsfiddle here - http://jsfiddle.net/5z2vu47a/

It's a simple timer that should change text that taken from and array

The timer only works once.

Why does the timer not continue to work.

var name_arr = ['learn', 'product', 'buying', 'selling', 'marketing', 'managing', ];
var counter = 0;

function names(){
    alert(counter);
    $('.text p').text(name_arr[counter]);
    counter++;
    alert(counter);
}

setTimeout(names, 1000);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use setInterval

setInterval(names, 1000);

In the names function, also check that the counter value should not exceed the name_arr.length.

function names(){
    alert(counter);
    $('.text p').text(name_arr[counter]);
    if(counter<(name_arr.length-1))
     counter++;
    else
     counter=0;
    alert(counter);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...