Sky Yip

On aspx.cs file:

jscript += "<script language=\x22" + "javascript\x22>\n";
jscript += "var x;\n";
jscript += "x=parseInt(p.style.left);\n";
jscript += "shift_image();\n";
jscript += "function shift_image(){\n";
jscript += "x=x-10;\n";
jscript += "if(x<10)\n";
jscript += "x=500;\n";
jscript += "p.style.left=x;\n";
jscript += "setTimeout(\x22" + "shift_image()\x22" + ",100);\n";
jscript += "}\n";
jscript += "</script>\n";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "js", jscript);

on aspx file:

<div id=p style="position:absolute;top:120;left:500;">
<img border="0" src="butterfly.gif" width="98" height="46">
</div>

When I run it, it can't move.




Re: JScript for the .NET Framework setTimeout does not work in asp.net

troy2

The main thing was that you refered to "p" without actually getting a hold of the object you wanted. Since this code runs entirely on the client, I would suggest just using client code like this:

<div id=p style="position:absolute;top:120px;left:500px;">
<img border="0" src="butterfly.gif" width="98" height="46">
</
div>

<script language="javascript">

var
graphic = document.getElementById("p");
var x = parseInt(graphic.style.left);
shift_image();

function shift_image() {
x -= 10;
if (x < 10) x = 500;
graphic.style.left = x +
"px";
setTimeout(shift_image, 100);
}

</
script>

Notice how I added "px" to the x variable which forces it to a string and I used document.getElementByID("p") to get a hold of the div tag.





Re: JScript for the .NET Framework setTimeout does not work in asp.net

Sky Yip

First, Thank you for your help. But it's not work, the original code is work on independ html file. when I put into my c# project and run in debug mode, it not work.




Re: JScript for the .NET Framework setTimeout does not work in asp.net

troy2

Follow these steps:

1) Create a new C# Web project
2) Open the Default.aspx page
3) Cut the code out above
4) Past it over the code in the Default.aspx page (leave the very top line in place)
5) Right-click on the page and choose to "View in Browser"

It works for me. I am guessing you didn't view it in the browser...





Re: JScript for the .NET Framework setTimeout does not work in asp.net

Sky Yip

Thank you very much,

It's solve the problem. My mistake is write the code on .aspx.cs file. It doesn't work.

You teach me so much.