473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

clearTimeout() help please

Hi, it's been about 5 years since I did JavaScript so I was wondering if I
could get some help with a little problem. I wrote a script from memory (5
years!!) and the only problem I have is clearing the timeout. Please advise:

<script language="JavaScript">

var myvar = 0

// Initiate is called onmouseover
function initiate() {
images = new Array
images[0] = "11210.jpg"
images[1] = "11211.jpg"
images[2] = "11212.jpg"
var image = images[myvar]
document.data.src = image
// If the images haven't reached the end keep going
if (myvar<3) {
myvar++
setTimeout("initiate()",500);
// otherwise start over
} else {
myvar=0
setTimeout("initiate()",500);
}
}
// Stop the timeouts!
function stop() {
// This doesn't stop the timeouts
clearTimeout()
}
</script>
Jul 23 '05 #1
2 1863
Adrian MacNair wrote on 26 apr 2005 in comp.lang.javascript:
Hi, it's been about 5 years since I did JavaScript so I was wondering
if I could get some help with a little problem. I wrote a script from
memory (5 years!!) and the only problem I have is clearing the
timeout. Please advise:

<script language="JavaScript">
That was 5 year ago

<script type="text/JavaScript">


var myvar = 0
var myTimeout;
// Initiate is called onmouseover
This is a bad idea, becaus succesive onmouseovers will
interfere with the setTimout call.
function initiate() {
The name wrongly suggests it is only called once
images = new Array
better use var here for appearance sake

var myImages = new Array();
images[0] = "11210.jpg"
images[1] = "11211.jpg"
images[2] = "11212.jpg"
Take these out of the function,
you dont want to define then every half second again
var image = images[myvar]
don't use possibly reserved names like "image",
you don't need this variable at all anyway
document.data.src = image
// If the images haven't reached the end keep going
if (myvar<3) {
myvar++
setTimeout("initiate()",500);
This you need:

myTimeout=setTimeout("initiate()",500);

// otherwise start over
} else {
myvar=0
setTimeout("initiate()",500);
}
}
// Stop the timeouts!
function stop() {
// This doesn't stop the timeouts
clearTimeout()
clearTimeout(myTimeout)
}
</script>


================================

so:

<script type="text/JavaScript">

// initation
var myvar = 0;
var myTimeout;
var running = false;
var myImages = new Array();
myImages[0] = "11210.jpg";
myImages[1] = "11211.jpg";
myImages[2] = "11212.jpg";

function start() { // onmouseover or button onclick?
if (!running) {
running=true;
nextimg();
}
};

function nextimg() {
document.getElementById('imgId').src = myImages[myvar];
myvar++;
if (myvar>2) myvar=0;
myTimeout = setTimeout("nextimg()",500);
};
function stop() { // other button onclick?
if (running) {
running=false;
clearTimeout(myTimeout);
};
};

</script>

not tested!!!
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
not tested!!!
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


Thanks very much. Your code is far superior and works quite superbly.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: masterofzen | last post by:
I haven't had occasion to use clearTimeout before, and this first go-round isn't going too well. Is there something obvious I'm doing wrong? Here, check this out: function reviewNick() { if...
2
by: joey.powell | last post by:
Hey guys I have some js code as follows... ------- var SessionTimer; function StartSessionTimer() { SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000) }
1
by: bizt | last post by:
Hi, I have a process where every so often I refresh a table's data using a setTimeout() .. and when its no longer needed, a clearTimeout(). The following is a simple example of how this is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.