$. Each () setTimeout () in the loop ()

Source: Internet
Author: User
$ (This ). find ('Article. loading '). each (function (I) {var El = This; setTimeout (function () {$ (EL ). replacewith ($ ('# dumpster article: First') ;}, speed );});

The preceding method only executes timeout once, and does not implement the expected latency of every each loop.

The correct method:

(1 ):

VaR elements = $ (this ). find ('Article. loading '); var Index = 0; setTimeout (function () {$ (elements ). get (index ). replacewith ($ ('# dumpster article: First'); index ++;}, speed );



VaR speed = 1000; // init timer and stores it's identifier so it can be unset latervar timer = setinterval (replacearticle, speed); var articles = $ ('Article. loading '); var length = articles. length; var Index = 0; function replacearticle () {articles. eq (index ). replacewith ($ ('# dumpster article: First'); index ++; // remove timer after interating through all articles if (index> = length) {clearinterval (timer );}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.