Jump to content
Sign in to follow this  
cherault

How to stop a function HTML - JAVASCRIPT

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,

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...