مشکل setInterval در مرورگر Chrome
با سلام خدمت تمامی دوستان و اساتید محترم
من یک اسلایدر نوشتم با setInterval تو حالت کلی مشکلی نداره . وقتی که با Chrome باز شده هم مشکلی نداره تا وقتی که صفحه رو Minimize می کنم . اگر صفحه برای 7 یا 8 دقیقه پایین پاشه کلا بهم میریزه و بر اساس زمان بندی کار رو انجام نمیده . دوستان تا حالا با این مشکل برخورد نداشتن؟
حتی وقتی که تب رو هم عوض می کنم همین مشکل به وجود میاد!
ممنون میشم راهنماییم کنید
با تشکر
نقل قول: مشکل setInterval در مرورگر Chrome
کلا این مشکل توی مرورگر های مدرن وجود داره،
شما وقتی از صفحه جاری که اسکریپتتون داره اجرا میشه خارج میشید، cpu برای اینکه به پردازش هایی که شما در حال حاضر روی اونا focus کردید
بهتر سرویس دهی کند، صفحات قبلی شما رو توی حالت معلق (wait) قرار میده،
حالا وقتی دوباره شما به صفحه اسکریپت خودتون برمیگردید (بعده 7-8 دقیقه)، cpu برنامهای که setInterval توش اجرا شده رو از صف معلقها (wait) به حالت اجرا (run) درمیاره .
برای همین همه زمانهایی که شما اسکریپت رو معلق گذاشتید رو اجرا میکنه !!
بطور کلی راهحل ها :
1- بیاد زمانی رو که از حالت focus اسکریپت خارج میشید رو تشخیص یدید، و setInterval رو با removeinterval غیر فعال کنید و وقتی دوباره برمیگردید دوباره setInterval !!
2- از یک سری تکنیک استفاده کنید مثلا مثال زیر هر 500ms یکبار اجرا میشه :
کد HTML:
<html>
<head>
<title>fix problem with setInterval and so </title>
<script type="text/javascript" src='../src/jquery.js'></script>
</head>
<body>
<div id='x' style='width:100px;height:100px'></div>
<script type="text/javascript">
var intervals = 1,
prev = new Date();
function timer() {
$('#x').css('background-color', intervals % 2 ? 'blue' : 'red');
intervals += 1;
//
// We want the next interval to be in 500ms, but if
// the previous one was too long we'll correct for it,
// or at least try to.
var now = new Date();
setTimeout(timer, Math.max(0, Math.min(500, 1000 - (now - prev))));
prev = now;
}
setTimeout(timer, 1000);
</script>
</body>
</html>
نقل قول: مشکل setInterval در مرورگر Chrome
یک راه دیگش هم اینه که از
.stop(true,true)
استفاده کنیم . این باعث میشه تمام بافرینگ صفر بشه.
با تشکر
نقل قول: مشکل setInterval در مرورگر Chrome
منظورتون رو متوجه نشدم چطوری با این method میتونید جلوی setInterval رو بگیرید ؟
با این method میشه جلوی انیمیشنی که روی selector-تون در حال اجراست رو بگیرید !
درسته ؟
نقل قول: مشکل setInterval در مرورگر Chrome
نقل قول:
منظورتون رو متوجه نشدم چطوری با این method میتونید جلوی setInterval رو بگیرید ؟
با این method میشه جلوی انیمیشنی که روی selector-تون در حال اجراست رو بگیرید !
درسته ؟
بله کاملا درسته
و چون من توی setinterval چند تا انیمیت بیشتر نداشتم با همین کد جواب رو گرفتم. البته کد شما هم درست جواب میداد.
با تشکر
نقل قول: مشکل setInterval در مرورگر Chrome
نقل قول:
نوشته شده توسط
مهدی رحیم زاده
یک راه دیگش هم اینه که از
.stop(true,true)
استفاده کنیم . این باعث میشه تمام بافرینگ صفر بشه.
با تشکر
ببخشید اگه ممکنه بگید از stop دقیقا باید کجای کدم استفاده کنم. مثلا این کد اسلایدشو من:
کد HTML:
setInterval(slider, 3000);
var next = 0;
function slider() {
if (next < 4) {
$('img').slideUp();
$('img').eq(next).slideDown();
next++;
} else {next = 0;}
}