Solved

How to interrupt javascript code?

Posted on 2001-08-16
13
2,035 Views
Last Modified: 2012-06-21
Hi!
I have the following code:
...
var Stopped = false;

function doSomething()
{
     Stopped = false;
     while(!Stopped) {
            // do something
     }
}

function Stop()
{
     Stopped = true;
}
...
Function "doSomething" is started when user clicks on a button "DO". After that, javascript code runs and the browser hangs. I would like to keep browser alive - so the user to be able to click on button "stop" that will call "stop" function that will stop "doSomething".
Is it possible to do that and how?

Thanks.
0
Comment
Question by:djokov
  • 7
  • 6
13 Comments
 
LVL 14

Expert Comment

by:avner
ID: 6393470
a completly different approach should be :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
//[avnerc@fundtech.co.il]
window.status=1
aa1=function(){aa=window.status;aa++;window.status=aa};
setInterval("aa1()",200);


//-->
</SCRIPT>
</HEAD>

<BODY>
</BODY>
</HTML>
0
 
LVL 14

Expert Comment

by:avner
ID: 6393473
you can also do :

moshe=setInterval("aa1()",200);

and then when you want to stop the repeating method :

clearInterval(moshe)


0
 
LVL 2

Author Comment

by:djokov
ID: 6393592
second approach is good.
however, i have arguments that I have to pass to aa() function. In javascript documentation is written that it should be done this way:

function aa(a, b)
{
...
}

function blabla(a1, b1)
{
moshe=setInterval("aa1()",200, a1, b1);
}

but it is doesn't work. if i try

moshe=setInterval("aa1(a1, b1)",200); it gaves error
if try
moshe=setInterval("aa1(1, 1)",200); it works

but i need to pass a1 and b1. how?
0
 
LVL 14

Expert Comment

by:avner
ID: 6393631
Change the method :

moshe=0
setInterval("aa1()",200);


function aa1()
{
window.status=moshe;
moshe++;
}


This way , when the moshe is a global param , you don't need to send it.


0
 
LVL 2

Author Comment

by:djokov
ID: 6393658
no way,
i know you are expert in XML also and you will understand.. :-)) this functions are connected with some data from xml files some of them are dynamicly genereted... etc.etc, i just need to pass this parameters. as i see in documentation, it should be possible, but i don't know why it doesn't run at my side.
0
 
LVL 14

Expert Comment

by:avner
ID: 6393670
Yes it should be.. it just some times a bit buggy ...
Let me try something, how about :


You said that this :

moshe=setInterval("aa1(a1, b1)",200); it gaves error

dosen't work , but where do you actually declar the values a1,b1 ?


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:avner
ID: 6393678
if you'll try :

a="moshe"
setInterval("aa1(a)",200);


function aa1(a1)
{
alert(a1);
}


the "moshe" will display , that means the variable is being passed.
0
 
LVL 2

Author Comment

by:djokov
ID: 6393703
No, see...
now i have this:

function blabla(a1, b1)
{
if (Stopped) {
   Stopped = false;
   run = setInterval('aa(a1, b1)',25);
}
}

when i start it i got error - "a1 is undefined". even if i use try:

function blabla(a1, b1)
{
var a=a1;
var b=b1;
if (Stopped) {
   Stopped = false;
   run = setInterval('aa(a, b)',25);
}
}
the result is error...
0
 
LVL 2

Author Comment

by:djokov
ID: 6393712
... this time "a is undefined"?!?
0
 
LVL 14

Accepted Solution

by:
avner earned 200 total points
ID: 6393731
Cam you use this approach :

<SCRIPT LANGUAGE="JavaScript">
<!--
var a;
var b;
Stopped=true
blabla("66", "77")
function blabla(a1, b1)
{
a=a1;
b=b1;
if (Stopped) {
  Stopped = false;
  run = setInterval('aa(a, b)',250);
}
}

function aa(r,t){window.status="["+r+","+t+"]"}

//-->
</SCRIPT>
0
 
LVL 2

Author Comment

by:djokov
ID: 6393760
:-)) it works, but if you move
var a;
var b;
to the body of blabla you will get the error i told you...
ok, now is clear.
thanx
0
 
LVL 14

Expert Comment

by:avner
ID: 6393776
I guess you mixed between "local variables" and "global variables" .
But I guess it's all over now....:)
0
 
LVL 2

Author Comment

by:djokov
ID: 6393810
i didn't mix them... but when I have to do something in the last minute such things happens.. anyway.. ;-)

This script is used to rotate dynamicly created SVG 3D bar chart created only (!) with XSL with no extension function on huge xml data with terrible structure... can you image it!!! %-)

cheers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now