[Seaside] Clear/stop a periodic javascript process using Seaside?

Paul DeBruicker pdebruic at gmail.com
Tue Feb 1 14:18:45 UTC 2011

Thanks Johan.

I hadn't considered using an external javascript file.  In your example 
js file I'd have to call unregisterInterval() to clear the interval.  If 
I created multiple intervals on the same page, would the correct one get 
cleared without passing unregisterInterval the correct value for 
updateId?  My knowledge of JS is less than Smalltalk.

I think I can figure out how to store the updateId using the 
jQuery.data() function on the DOM element that is updating then send the 
clearInterval with the result of the calculation and have it get the 
updateId from the DOM element.

I think it'd be something like this:

function registerAndSetInterval(aLambda, ms, anElement){
	var updateId;
	updateId := setInterval(aLambda, ms);
	$.data(anElement, "intervalId", updateId);

funtion unregisterInterval(anElement){
	var clearId;
	clearId:= $.data(anElement, "intervalId");

But I'd probably want some kind of timeout in the first function so it 
stops if the connection drops or something goes haywire.

Thanks for the heads up on the renderContentOn: method I posted.  I had 
it in the email using replaceWith: and switched to load html: in an 
attempt to simplify the problem illustration.


On 02/01/2011 07:00 AM, seaside-request at lists.squeakfoundation.org wrote:
> Message: 4
> Date: Tue, 1 Feb 2011 08:34:46 +0100
> From: Johan Brichau<johan at inceptive.be>
> Subject: Re: [Seaside] Clear/stop a periodic javascript process using
> 	Seaside?
> To: Seaside - general discussion<seaside at lists.squeakfoundation.org>
> Message-ID:<2A77B15A-BD63-4C83-8549-0582F9A3D913 at inceptive.be>
> Content-Type: text/plain; charset=us-ascii
> You can assign the id that is returned by setInterval() to a global variable and call clearInterval() whenever you want to.
> The best thing to do in this case is use an external javascript file where you declare the (global) variable that should keep the reference id (at least) and also the functions that will register the id and cancel the periodic update. You then call these functions from within the Seaside-generated javascripts. Although you can probably make it work without the external javascript file, it's better practice imho. Here's what I would do:
> var updateId;
> function registerAndSetInterval(aLambda,ms) {
> 	updateId = setInterval(aLambda,ms);
> }
> function unregisterInterval(){
> 	clearInterval(updateId);
> }
> Next, I'm a bit puzzled with your Seaside code. Doesn't that create a nested div inside the already existing div every 2 seconds? Since the load block creates another div with the same id, you will run into problems. You also need to use the canvas object that is passed to the html load block instead. Here is what I make of it:
> renderContentOn: html
> 	(html div)
> 		id: 'replace';
> 		script:  (JSStream on: 'registerAndSetInterval')
> 					call: ''
> 					with: ((html jQuery id:'replace')
> 								load html: [ :h |
> 											count := count + 1.
> 											h render: 'count: ' , count greaseString ]) asFunction
> 					with: 2000
> Calling a javascript function from Seaside-generated javascript is a bit awkward using the #call:with: because it was designed to be sent to a receiver object (such as jQuery instance). But I don't think there's another way to call them from Seaside-generated javascript.
> Hope it works, if not: please let me know;-)
> Johan

More information about the seaside mailing list