Javascript 停止间隔和之后的动作
Javascript stop interval and action after
我的问题与 setintervall 函数有关。当 first()
和 second()
执行完成时,我调用一个名为 thrice()
的函数。这没有问题。下面的代码:
var oneFinish = false;
var twoFinish = false;
function first() {
console.log("FUNCTION first RUNNING");
for (i = 0; i < 5; i++) {
console.log("first " + i);
}
console.log("FUNCTION first FINISH");
oneFinish = true;
}
function second() {
console.log("FUNCTION second RUNNING");
for (i = 0; i < 10; i++) {
console.log("second " + i);
}
console.log("FUNCTION second FINISH");
twoFinish = true;
}
function thrice() {
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
}
}, 3000);
console.log("FUNCTION thrice FINISH");
}
first();
second();
thrice();
输出是这样的:
FUNCTION first RUNNING
first 0
first 1
first 2
first 3
first 4
FUNCTION first FINISH
FUNCTION second RUNNING
second 0
second 1
second 2
second 3
second 4
second 5
second 6
second 7
second 8
second 9
FUNCTION second FINISH
FUNCTION thrice FINISH
FUNCTION thrice RUNNING
你看输出的end,问题是FUNCTION thrice FINISH
在FUNCTION thrice RUNNING
之前执行
那是因为你在 3 秒后登录开始,但你立即登录结束。试试这个:
function thrice() {
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
console.log("FUNCTION thrice FINISH");
}
}, 3000);
}
因为,setInterval中函数的所有内容都是在3000毫秒后调用的。它的目标是 setInterval :http://www.w3schools.com/jsref/met_win_setinterval.asp
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
}
}, 3000);
console.log("FUNCTION thrice FINISH");
如果你想固定订单,你必须把 console.log("FUNCTION thrice FINISH");
放在回调函数中:
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
console.log("FUNCTION thrice FINISH");
}
}, 3000);
我的问题与 setintervall 函数有关。当 first()
和 second()
执行完成时,我调用一个名为 thrice()
的函数。这没有问题。下面的代码:
var oneFinish = false;
var twoFinish = false;
function first() {
console.log("FUNCTION first RUNNING");
for (i = 0; i < 5; i++) {
console.log("first " + i);
}
console.log("FUNCTION first FINISH");
oneFinish = true;
}
function second() {
console.log("FUNCTION second RUNNING");
for (i = 0; i < 10; i++) {
console.log("second " + i);
}
console.log("FUNCTION second FINISH");
twoFinish = true;
}
function thrice() {
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
}
}, 3000);
console.log("FUNCTION thrice FINISH");
}
first();
second();
thrice();
输出是这样的:
FUNCTION first RUNNING
first 0
first 1
first 2
first 3
first 4
FUNCTION first FINISH
FUNCTION second RUNNING
second 0
second 1
second 2
second 3
second 4
second 5
second 6
second 7
second 8
second 9
FUNCTION second FINISH
FUNCTION thrice FINISH
FUNCTION thrice RUNNING
你看输出的end,问题是FUNCTION thrice FINISH
在FUNCTION thrice RUNNING
那是因为你在 3 秒后登录开始,但你立即登录结束。试试这个:
function thrice() {
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
console.log("FUNCTION thrice FINISH");
}
}, 3000);
}
因为,setInterval中函数的所有内容都是在3000毫秒后调用的。它的目标是 setInterval :http://www.w3schools.com/jsref/met_win_setinterval.asp
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
}
}, 3000);
console.log("FUNCTION thrice FINISH");
如果你想固定订单,你必须把 console.log("FUNCTION thrice FINISH");
放在回调函数中:
var intev = setInterval(function () {
if (oneFinish && twoFinish) {
console.log("FUNCTION thrice RUNNING");
oneFinish = false;
twoFinish = false;
clearInterval(intev);
console.log("FUNCTION thrice FINISH");
}
}, 3000);