var k=0;var n=0;
function shiftrigh(){
n=n+1;
if(n<=193)
window.setTimeout(shiftright(),100);
else
n=0;}
function shiftright(){
k-=1;
document.getElementById("abcmngcontainer").style.left=k+"px";
window.setTimeout(shiftrigh(),100);
}
function shiftlef(){
n=n+1;
if(n<=193)
window.setTimeout(shiftleft(),100);
else
n=0;}
function shiftleft(){
k+=1;
document.getElementById("abcmngcontainer").style.left=k+"px";
window.setTimeout(shiftlef(),100);
}
Run Code Online (Sandbox Code Playgroud)
嗨,我有上面的代码.该函数shiftrigh调用时所调用shiftright,然后循环创建,然后一直持续到n是193.这shiflef对的情况也是如此.代码正在运行,但它工作得很快.无论我是减少settimeout中的时间值还是增加它,它都保持不变.更新速度非常快,不够顺畅.
更改:
window.setTimeout(shiftright(),100);
Run Code Online (Sandbox Code Playgroud)
至:
window.setTimeout(shiftright,100);
Run Code Online (Sandbox Code Playgroud)
注意丢失的parens.与shiftleft()- > 相同shiftleft.
这是JavaScript中常见的误解.setTimeout()需要引用函数(shiftright).你正在做的是立即调用该函数并传递从它返回的任何函数setTimeout().显然不是意图.