Solved

How to interrupt javascript code?

Posted on 2001-08-16
13
2,020 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Expert Comment

by:avner
Comment Utility
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
Comment Utility
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
Comment Utility
... this time "a is undefined"?!?
0
 
LVL 14

Accepted Solution

by:
avner earned 200 total points
Comment Utility
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
Comment Utility
:-)) 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

762 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

7 Experts available now in Live!

Get 1:1 Help Now