Tôi đã tạo một vòng lặp "Thay đổi từ" bằng jQuery bằng cách sử dụng mã trong câu trả lời này: jQuery: tìm từ và thay đổi cứ sau vài giây
Làm thế nào để tôi dừng nó sau một thời gian? Nói sau 60 giây hoặc sau khi nó đi qua vòng lặp?
[function[] {
// List your words here:
var words = [
'Lärare',
'Rektor',
'Studievägledare',
'Lärare',
'Skolsyster',
'Lärare',
'Skolpsykolog',
'Administratör'
],
i = 0;
setInterval[function[] {
$['#dennaText'].fadeOut[function[] {
$[this].html[words[i = [i + 1] % words.length]].fadeIn[];
}];
// 2 seconds
}, 2000];
}][];
Ivar
5.72412 Huy hiệu vàng52 Huy hiệu bạc59 Huy hiệu Đồng12 gold badges52 silver badges59 bronze badges
hỏi ngày 3 tháng 2 năm 2012 lúc 22:15Feb 3, 2012 at 22:15
Để dừng nó sau khi chạy một số lần đã đặt, chỉ cần thêm một bộ đếm vào khoảng thời gian, sau đó khi nó đạt đến số đó xóa nó.
e.g.
var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
Nếu bạn muốn dừng nó sau một thời gian đã thiết lập đã trôi qua [ví dụ: 1 phút], bạn có thể làm:
var startTime = new Date[].getTime[];
var interval = setInterval[function[]{
if[new Date[].getTime[] - startTime > 60000]{
clearInterval[interval];
return;
}
//do whatever here..
}, 2000];
Đã trả lời ngày 3 tháng 2 năm 2012 lúc 22:20Feb 3, 2012 at 22:20
Mark Rhodesmark RhodesMark Rhodes
9.8234 huy hiệu vàng46 Huy hiệu bạc51 Huy hiệu đồng4 gold badges46 silver badges51 bronze badges
6
Sử dụng
var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
2 để xóa khoảng. Bạn cần vượt qua ID khoảng mà bạn nhận được từ phương thức var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
3.E.g.
var intervalId = setInterval[function[]{
....
}, 1000];
Để xóa cách sử dụng khoảng trên
clearInterval[intervalId];
Bạn có thể thay đổi mã của bạn như dưới đây.
[function[]{
// List your words here:
var words = [
'Lärare',
'Rektor',
'Studievägledare',
'Lärare',
'Skolsyster',
'Lärare',
'Skolpsykolog',
'Administratör'
], i = 0;
var intervalId = setInterval[function[]{
$['#dennaText'].fadeOut[function[]{
$[this].html[words[i=[i+1]%words.length]].fadeIn[];
if[i == words.length]{//All the words are displayed clear interval
clearInterval[intervalId];
}
}];
// 2 seconds
}, 2000];
}][];
Đã trả lời ngày 3 tháng 2 năm 2012 lúc 22:17Feb 3, 2012 at 22:17
ShankarsangolishankarsangoliShankarSangoli
69.1K12 Huy hiệu vàng91 Huy hiệu bạc123 Huy hiệu đồng12 gold badges91 silver badges123 bronze badges
0
Giải pháp đơn giản nhất là
var intervalId = setInterval[function[] {
$['#dennaText'].fadeOut[function[] {
$[this].html[words[i = [i + 1] % words.length]].fadeIn[];
}];
}, 2000]; // run every 2 seconds
setTimeout[function[]{
clearInterval[intervalId];
},10000] // stop it after 10seconds
Đã trả lời ngày 2 tháng 2 năm 2021 lúc 21:30Feb 2, 2021 at 21:30
Bạn nên xem xét sử dụng một đệ quy
var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
4 thay vì var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
5 để tránh tình trạng cuộc đua.var fadecount = 1;
[function interval[]{
$['#dennaText'].fadeOut[function[]{
$[this].html[words[i=[i+1]%words.length]].fadeIn['fast',function[]{
if [fadecount < 30]{
fadecount += 1;
setTimeout[interval, 2000];
}
}];
}];
}[]];
Đã trả lời ngày 3 tháng 2 năm 2012 lúc 22:22Feb 3, 2012 at 22:22
AlienwebguiyalienwebguyAlienWebguy
76.2K17 Huy hiệu vàng121 Huy hiệu bạc144 Huy hiệu đồng17 gold badges121 silver badges144 bronze badges
3
Thay vào đó, bạn có thể sử dụng
var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
6, điều này tốt hơn:[function foo[]{ // wrap everything in a self-invoking function, not to expose "times"
times = 20; // how many times to run
[function run[]{
// do your stuff, like print the iteration
document.body.innerHTML = times;
if[ --times ] // 200 * 20 = 4 seconds
setTimeout[run, 100];
}][];
}][];
Đã trả lời ngày 3 tháng 4 năm 2014 lúc 9:22Apr 3, 2014 at 9:22
vsyncvsyncvsync
Huy hiệu vàng 110K5454 gold badges291 silver badges374 bronze badges
2
Tôi đang làm việc với Vuejs và muốn loại bỏ một div sau vài giây; Đây là những gì tôi đã làm, tôi hy vọng điều đó có thể giúp ai đó;].
export default {
name: "Home",
components: {},
data[] {
return {
removeAlert: false,
};
},
methods: {
removeAlertF[] {
let wait = window.setInterval[[] => {
this.removeAlert = true;
console.log["done"];
if[true]{
window.clearInterval[wait];
}
}, 3000];
},
},
mounted[] {
this.loadData[];
this.removeAlertF[];
},
};
var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
0var timesRun = 0;
var interval = setInterval[function[]{
timesRun += 1;
if[timesRun === 60]{
clearInterval[interval];
}
//do whatever here..
}, 2000];
1Đã trả lời ngày 27 tháng 2 năm 2021 lúc 20:19Feb 27, 2021 at 20:19
Walid Boussetawalid BoussetaWalid Bousseta
1.2592 huy hiệu vàng16 Huy hiệu bạc33 Huy hiệu đồng2 gold badges16 silver badges33 bronze badges