PDA

View Full Version : Making a widget run continuously


Gretch
2008.06.04, 08:46 PM
I have created a widget I want to run continuously. The prototype works fine but I cannot get it to run more than once.

I have tried enclosing the code in a while statement, but it just hangs.

function myWidgetCode()
{
i=1;
while (i=1);
{alert ("looping...") // my widget code goes here
}
}

This function is added to the end of the .js file and I execute it as part of the load function which is executed via the html onload="load();

function load()
{
setupParts();
myWidgetCode();
}

I also want this widget to loop continuously even when the dashboard is not present. It needs to run in the background. It will start and stop when added to the dashboard.

What do I need to do to make my widget run continuously? This is my first widget and first time coding with javascript.

Thanks in advance for your help.

backslash
2008.06.05, 07:30 AM
You might want to brush up on a few basics of syntax. Your code should have said:
function myWidgetCode()
{
i=1;
while (i==1)
{
alert ("looping..."); // my widget code goes here
}
}
I have never written a widget myself, but I would imagine you should probably use a repeating timer to trigger your function once every second (or whatever is appropriate) rather than jamming it in a while loop which tends to seize rather more processor cycles than required.

Gretch
2008.06.06, 12:54 AM
Thanks for the correction and I took your advice and used a timer:

setInterval ( "myWidgetCode()", 1000 );

Simple when you know what your are doing!

Gretch
2008.06.06, 11:42 PM
I have succeeded in making my widget run in the Dashcode development environment. I implemented the recommendation to use a timer function.
I placed the following in my html file.

<body onload="load(); setInterval( 'mywidget()', 1000 )">

The mywidget function is in the same .js file as the load function.

It runs perfectly in the Dashcode environment and does everything it is supposed to do.

However when I Deploy the Widget to the Dashboard, it shows up, but does nothing. I even inserted an alert function into the load function and nothing happens.

Am I missing a step in deploying a widget?

Thanks in advance.