日期:2014-05-18  浏览次数:20704 次

js函数调用
我想通过setTimeout方法来控制js某个函数的执行次数,当某个js函数的执行超过指定次数时,自动跳转并执行其他函数。
如:setTimeout(ourmove,1),ourmove是我的js函数,然后就不知道怎么写了,求各位高手帮忙!谢谢!

------解决方案--------------------
Java code

var count = 0;
var toCount = 10;//指定次数
function ourmove(){
    if (count < toCount) {
        count++;
        //ourmove函数要执行的代码
    } else {
        anotherFunction();
    }
}
function anotherFunction() {...}
setInterval(ourmove,1);

------解决方案--------------------
定义一个int变量,js函数每执行一次加一,然后用变量作判断不就行了吗。
------解决方案--------------------
JScript code
var count = 0;
var toCount = 10;//指定次数
function ourmove(){
    if (count < toCount) {
        count++;
        //ourmove函数要执行的代码
    } else {
        clearInterval(itid); 
        anotherFunction();
    }
}
function anotherFunction() {...}
var itid = setInterval(ourmove,1);