setTimeout doesn't run synchronously

后端 未结 1 375
花落未央
花落未央 2021-01-25 10:03

I\'m trying to make a function to show some messages after 2 seconds in a \'for\' function, but seems like isn\'t running in order at all, if I set one time higher, it will not

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 10:51

    it's an async callback, call the next setTimeout from within the callback of the first.

    time += 2000;
    (function(set, cName, time) {
        if (cName == "A" || cName == "B") {
            setTimeout(function() {
                text.innerHTML = "somethibng 
    "+text.innerHTML; set.setAttribute("class", "type"+cName ); }, time); } else { setTimeout(function() { text.innerHTML = "something else
    "+text.innerHTML; set.setAttribute("class", "type"+cName ); /******* call second setTimeout once the first one finished ***/ setTimeout(function() { text.innerHTML = "and then another text
    "+text.innerHTML; }bind(), time); /**** notice the bind, it's not mandatory (unless you pass vars to the second timeer) **/ }), time); } })(set, cName, time);

    0 讨论(0)
提交回复
热议问题