javascript - setTimeout doesn't run synchronously -
i'm trying make function show messages after 2 seconds in 'for' function, seems isn't running in order @ all, if set 1 time higher, not wait until done , next task. how make settimeout wait finish before starting new one?
time += 2000; (function(set, cname, time) { if (cname == "a" || cname == "b") { settimeout(function() { text.innerhtml = "somethibng <br />"+text.innerhtml; set.setattribute("class", "type"+cname ); }, time); } else { settimeout(function() { text.innerhtml = "something else <br />"+text.innerhtml; set.setattribute("class", "type"+cname ); }, time); settimeout(function() { text.innerhtml = "and text <br />"+text.innerhtml; }, time); } })(set, cname, time);
it's async callback, call next settimeout within callback of first.
time += 2000; (function(set, cname, time) { if (cname == "a" || cname == "b") { settimeout(function() { text.innerhtml = "somethibng <br />"+text.innerhtml; set.setattribute("class", "type"+cname ); }, time); } else { settimeout(function() { text.innerhtml = "something else <br />"+text.innerhtml; set.setattribute("class", "type"+cname ); /******* call second settimeout once first 1 finished ***/ settimeout(function() { text.innerhtml = "and text <br />"+text.innerhtml; }bind(<pass things second timeout if need>), time); /**** notice bind, it's not mandatory (unless pass vars second timeer) **/ }), time); } })(set, cname, time);
Comments
Post a Comment