我想创建一个新窗口并自动(使用setInterval)将此窗口移动到新位置。 我尝试了这段代码但似乎只执行了setInterval中定义的函数一次......
<!doctype html>
<html>
<head>
<title> BOM </title>
</head>
<body>
<script type="text/javascript">
var newWin = window.open();
var xcor=0;
var ycor=0;
newWin.resizeTo(500,500);
newWin.document.write("<p>This a new window. I am 500px wide and 500px tall!</p> \n");
setInterval(move(newWin, xcor+=30,ycor+=30),1000);
function move(item,x,y) {
item.moveTo(x,y);
}
</script>
</body>
</html>
有什么建议吗?
根据teemu的评论,我对代码进行了以下更改
<!doctype html>
<html>
<head>
<title> BOM </title>
</head>
<body>
<script type="text/javascript">
var newWin = window.open();
var x=0;
newWin.resizeTo(500,500);
newWin.document.write("<p>This a new window. I am 500px wide and 500px tall!</p> \n");
setInterval(move(),1000);
function move() {
alert(x+=30);
}
</script>
</body>
</html>
再次功能移动(实际上只是一个警报)只执行一次?还有其他意见吗?