A small problem with JQuery slideshow (how to discover and resolve the process) _jquery

Source: Internet
Author: User
first stage, try slideshow
In doing a Web page homepage, want to use slideshow[1] do picture switching effect, which JS part of the code as follows:
Copy Code code as follows:

function Next_slide () {
var $active = $ (' #bitware-overlay img.active ');
if ($active. length = 0)
$active = $ (' #bitware-overlay img:last ');
var $next = $active. Next (). length? $active. Next (): $ (' #bitware-overlay img:first ');
$active. addclass (' last-active ');
$next. addclass (' active ');
$next. css ({opacity:0.0});
$next. Animate ({opacity:1.0}, 1500,function () {
$active. Removeclass ();
});
}
$ (function () {
SetInterval ("Next_slide ()", 4000);
});

second stage, finding problems
In the open page so the normal test is no problem, finally, the leader found that when the browser at the same time to open more than one tab, the page in his tab after a period of time, will appear the picture is the last picture, and then change to the first picture, just change (with the effect of fading) completed, suddenly jump to the last picture. When I changed to:
Copy Code code as follows:

function Next_slide () {
var $active = $ (' #bitware-overlay img.active ');
if ($active. length = 0)
$active = $ (' #bitware-overlay img:last ');
var $next = $active. Next (). length? $active. Next (): $ (' #bitware-overlay img:first ');
$active. addclass (' last-active ');
$next. addclass (' active ');
$next. css ({opacity:0.0});
$next. Animate ({opacity:1.0}, 1500);
$active. Removeclass ();
SetTimeout ("Next_slide ()", 4000);
}
$ (function () {
SetTimeout ("Next_slide ()", 4000);
});

found that the rest of the tab, back to the page, the picture is displayed in the correct order, interval is correct, 4000 milliseconds, but when shown, is a direct jump, nor fade out shape, it seems that animate 1500 milliseconds did not start any effect. After a period of time will return to normal, but this phenomenon of the length of time, with the rest of the tab time seems to be proportional to the length.
Phase III, problem solving
The final reason for this is that jquery's nature is single-threaded [2], and when stuck in another tab, the Next_silde () function in the task will accumulate too much. The Final solution found [4] is as follows:
Copy Code code as follows:

function Next_slide () {
var $active = $ (' #bitware-overlay img.active ') ;
if ($active. length = = 0)
$active = $ (' #bitware-overlay img:last ');
var $next = $active. Next (). Length $active. Next (): $ (' #bitware-overlay img:first ');
$active. addclass (' last-active ');
$next. addclass (' active ');
$next. css ({opacity:0.0});
$next. Animate ({opacity:1.0}, 1500,function () {
$active. removeclass ();
settimeout ("Next_slide ()", 4000);
});
}
$ (function () {
settimeout ("Next_slide ()", 4000);
});

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.