在Javascript中使用setTimeout移动文本不起作用

时间:2016-03-12 01:38:47

标签: javascript html5

您好我正在做一个教程和文本" Yoo"假设向右移动但它没有。 Ty

<!DOCTYPE html>
<html>
<head>
<script>
    var timer, x_pos=0, txt;
    function _timer() {
        txt = document.getElementById("txt");
        x_pos = x_pos+1;
        txt.style.left = x;
        timer = setTimeout(_timer, 50);
    }
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

永远不会定义变量x。尝试:

txt.style.left = x_pos;

而不是

txt.style.left = x;

这样你的最终代码就是

<!DOCTYPE html>
<html>
<head>
<script>
    var timer, x_pos=0, txt;
    function _timer() {
        txt = document.getElementById("txt");
        x_pos = x_pos+1;
        txt.style.left = x_pos;
        timer = setTimeout(_timer, 50);
    }
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>

答案 1 :(得分:0)

永远不会定义变量x,您应该在left中提供px作为

txt.style.left = x_pos + "px";

<!DOCTYPE html>
<html>
<head>
<script>
    var timer, x_pos=0, txt;
    function _timer() {
        txt = document.getElementById("txt");
        x_pos = x_pos+1;
        txt.style.left = x_pos + "px";
        timer = setTimeout(_timer, 50);
    }
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>

答案 2 :(得分:0)

你忘了添加单位:)

packet *test = new packet(1, 4, 4, message); //message is a *char with the data
test->serialize(sendbuf);          //this works correctly
packet *test2 = new packet(0,0,0, NULL);  //I am not sure if I need to be creating a new packet for the deserialized information to get placed into
test->deserialize(sendbuf);    //results in a segmentation fault currently