Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2005, 08:59 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Animation

Hi,

I am trying to learn how to use the setInterval to call a functin and animate a cart but I keep getting the following error:

"There is no source code available for the current location"

I think the problem is happening when I call the function - vartimer = window.setInterval('moveCart()', 100); to set the vartimer.

Also the cart is not moving and sometimes I get an error saying cart is undefined.

I cannot figure out what is wrong and would appreciate if someone could help.

Cheers,

Claudio

<script type="text/javascript">
<!--
    vartimer = 0;
    var incr = 10;

    function moveCart()
    {
        var xPos;
        xPos = cart.style.posLeft + incr;
        if(xPos >= 400)
        {
            incr = -10
            cart.style.filter = "FlipH()";
        }
        if(xPos <= 20)
        {
            incr = 10;
            cart.style.filter = "";
        }
        cart.style.posLeft = xPos;
    }

    function toggleAnim()
    {

        //window.setInterval('moveCart()',100);
        //alert(window.setInterval('moveCart()', 100));

        if(vartimer!=0)
        {
            window.clearInterval(vartimer);
            cmdAnimate.innerHTML = "<input type='button' value='Animation On' onclick='toggleAnim()'>";
            vartimer = 0;
        }
        else
        {
            vartimer = window.setInterval('moveCart()', 100);
            cmdAnimate.innerHTML = "<input type='button' value='Animation Off' onclick='toggleAnim()'>";
        }

    }



-->
</script>

 
Old February 1st, 2005, 09:30 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you show a bit more of the page?

--

Joe (Microsoft MVP - XML)
 
Old February 1st, 2005, 09:50 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi joefawcett,

Sure. below is the whole page. Just find a cart graphic and copy into an image folder. I think the cart should me moving but it isn't this is quite annoying since I cannot spot any errors at all. Thanks for your help.

<html>
  <head>
    <title>setIntervalCSSAnimation</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
    <style>
    H2, P {font-family:tahoma}
    </style>

<script type="text/javascript">
<!--
    vartimer = 0;
    var incr = 10;

    function moveCart()
    {

        var xPos;
        xPos = cart.style.posLeft + incr;
        if(xPos >= 400)
        {
            incr = -10
            cart.style.filter = "FlipH()";
        }
        if(xPos <= 20)
        {
            incr = 10;
            cart.style.filter = "";
        }
        cart.style.posLeft = xPos;
    }

    function toggleAnim()
    {

        //window.setInterval('moveCart()',100);
        //alert(window.setInterval('moveCart()', 100));

        if(vartimer!=0)
        {
            window.clearInterval(vartimer);
            cmdAnimate.innerHTML = "<input type='button' value='Animation On' onclick='toggleAnim()'>";
            vartimer = 0;
        }
        else
        {
            vartimer = window.setInterval('moveCart()', 100);
            cmdAnimate.innerHTML = "<input type='button' value='Animation Off' onclick='toggleAnim()'>";
        }

    }



-->
</script>


</head>



<body MS_POSITIONING="GridLayout">
<h2>Animation</h2>
<p>Click button to move cart</p>

<span id="cart" NAME="cart" style="postion:absolute; top:150px; lef:20px">
    <img src="images/cart.jpg">
</span>

<form name="frmMain" method="post" runat="server" action="setIntervalCSSAnimation.aspx">
    <span id="cmdAnimate">
        <input type="button" value="Animation on" onclick="toggleAnim()">
    </span>
</form>

</body>
</html>

 
Old February 2nd, 2005, 06:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You have mispelled 'position' and 'left' in your style attribute of 'cart' span.

--

Joe (Microsoft MVP - XML)
 
Old February 2nd, 2005, 07:59 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Thanks Joe.

Is there a way to debug for mispelling like that??

 
Old February 2nd, 2005, 08:03 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You could also simplify toggleAdmin.
Code:
function toggleAdmin(Sender)
{
  if(vartimer!=0)
  {
    window.clearInterval(vartimer);
    Sender.value = "Animation On";            
    vartimer = 0;
  }
  else
  {
    vartimer = window.setInterval('moveCart()', 100);
    Sender.value = "Animation Off";
  }
}
and change your button html to:
Code:
<input type="button" value="Animation on" onclick="toggleAnim(this);">
--

Joe (Microsoft MVP - XML)
 
Old February 2nd, 2005, 08:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

What is "Sender.value" ?

I can see you are changing the value of the button but why Sender does that?? I would appreciate if you could elaborate on that.

Cheers,

Claudio

 
Old February 2nd, 2005, 10:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The "this" in "toggleAmin(this);" is a reference to the element that fired the event.

--

Joe (Microsoft MVP - XML)
 
Old February 2nd, 2005, 10:03 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by pallone
 Thanks Joe.

Is there a way to debug for mispelling like that??

Not in any editor I know, they can cope with attributes mispelt but not with inline styles.

--

Joe (Microsoft MVP - XML)
 
Old February 2nd, 2005, 03:17 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Thanks ever so much Joe,

Does Sender works for netscape as well ?

Cheers,

Claudio






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with animation 132591 Java Basics 0 December 8th, 2006 06:45 PM
Text Animation flash2004 Flash (all versions) 2 May 18th, 2005 12:41 AM
Animation Control JelfMaria VB How-To 2 March 14th, 2005 12:11 AM
animation problems chrissy Javascript 1 February 22nd, 2005 12:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.