Jump to content

How to stop a function HTML - JAVASCRIPT


cherault

Recommended Posts

Dear all,

Using a small webpage for my app, I am confused because I don't how to stop the function I did.

I am using a C++ program which grab pictures in a file.
I read the file and update it each 2 seconds.

The functions I used are:

<script language="javascript">
function updateImage() 
{
   obj = document.imagename;
   obj.src = obj.src + "?" + Math.random();
   setTimeout("updateImage()",2000);
}
</script>

<script language="javascript">
function showPicture() 
{
	var sourceOfPicture = "/home/tux/Prog/Medias/Images/calib1.png";
  	var img = document.getElementById('bigpic')
  	img.src = sourceOfPicture.replace('90x90', '225x225');
  	img.style.display = "block";
} 
</script>

And I call them like this:

	<h1>Camera Preview</h1>
	   <button onclick="showPicture()">Start Preview</button><br><br>
		<body onload="updateImage()">
		<img style="display:block;" id="bigpic" src="bigpic" name="imagename" width="340" height="240">

Can you tell me how can I do to add a "stop" button to stop the update, and conserving the last image on screen ?

Thank you for your help and support.

Best regards,

Link to comment
Share on other sites

You save the return value from setTimeout, and use clearTimeout to cancel it.

obj.src = obj.src + "?" + Math.random();

That's going to produce something like this:

/home/tux/Prog/Medias/Images/calib1.png?123?456?789?...

A URL shouldn't have more than 1 question mark.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...