Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I add a default.aspx & .js file & Image Folder('contain a lot of image files') in web site.

My .aspx file code is below:-
----------------------
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server"  onload="LoadDataToText">
        <asp:Button ID="Button1" runat="server" Text="Press Me" OnClick="rotate" />
        <br />
        <script language="javascript" type="text/javascript" src="JS/ImgRotate.js" >
        </script>
        <img src="Image/Flower.jpg" width="400" height="400" id="adBanner" alt="AdBanner"/>
    </form>
</body>
</html>


My .js file code is below:-
-------------------------
JavaScript
window.onload = rotate;
var thisAd = 0;
var adImages = new Array("Image/ani0a.jpg", "Image/ani10a.jpg", "Image/ani11a.jpg");
function rotate()
{
     thisAd++;
     if (thisAd == adImages.length) 
     {
        thisAd = 0;
     }
     document.getElementById("adBanner").src = adImages[thisAd];
     setTimeout("rotate()", 3 * 1000);
}


Above code is work fine. Image are rotate one after one at interval 3sec.
But when i will locate the mouse on the image in that i will do stop the rotation & reversely, i will again start the rotation process.

How will i do that problem in onmouseover/onmouseout events using javascript.
But not a jquery.

Please Please help me anybody.....
Posted
Updated 8-Oct-12 2:22am
v2

1 solution

As it stands, every time the images are rotated you schedule the next time that they will be rotated. This makes it difficult (impossible?) to stop the slideshow.

The best way to do it would be to use setInterval instead.

That way, you

(a) can set a consistent 3 seconds between the start of rotate events (instead of starting the next event 3 seconds after you finish the current event)
(b) get an identifier that can be used to stop the timer.

So next, you simply call clearInterval when you get an mouseover event. Then you call setInterval once more when you get a mouseout event.

JavaScript
// starts the rotation
var rotateTimer = setInterval(rotate, intervalMs);

function mOver()
{
  clearInterval(rotateTimer);
}

function mOut()
{
  rotateTimer = setTimer(rotate, intervalMs);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900