I don’t know why the n times in it do not increase, and how to clear the execution after executing it three times.
var firstShow = 1000;
var secondShow = 5000;
var threeShow = 10000;
setTimeout(openMpM, firstShow);
function openMpM() {
$("#swtCenter2").fadeIn(1000);
}
var n = 0;
function closeM(n) {
$("#swtCenter2").fadeOut(1000);
setTimeout(openMpM, 50000);
n = n++;
if (n == 1) {
setTimeout(openMpM, secondShow);
}
if (n == 2) {
setTimeout(openMpM, threeShow);
}
if (n == 3) {
clearTimeout();
}
}
In the binding event, first n++ and then execute closeM(n). The n=n++ in closeM(n) must be removed.
For the third time, just if (n==3) {$("#swtCenter2").fadeOut(1000);} will do
or this
In fact, your idea is basically very clear, but the details are still a little unclear. For example,
setTimeout(openMpM, 50000);
This sentence will be executed every timecloseM()
, regardless of then
value.Also
n = n++
does not change then
value, it is equivalent toThe following is my modified code. This question is the same as the one you asked, so I won’t answer it there.
n = n++;
Due to n operator precedence, n has not changed
so it should be directly ++n;