The "update" function is executed twice

Hi everyone,

I work on an element as a plugin, in this plugin I face unexpected behaviors like that the “update” function is executed twice and the element rendered twice, to avoid this behavior I added this line to the “initialize” function:
instance.data.executed = false;
and these lines to the beginning of the "update function:
if (!instance.data.executed) {
instance.data.executed = true;

This solve the problem put is this normal behavior or I encounter a bug?

If you want to run something only one time, consider moving that code to initialise function rather than update.

Ankur@Nocodetalks
Checkout Bubble Plugin Development Course?

update will run any time there is a change to one of the data arguments being used by the plugin. This happens a lot during page load in particular and needs to be handled.

But in general it’s a useful behaviour that you can exploit in your plugin design.

Hi @ankur1 and @exception-rambler,

Actually I rely on the properties which is exist in the “update” function and not the “initialize” function and I aware the “update” function will run any time there is a change to one of the data arguments being used by the plugin, and I need this to happen, the problem is that any time this function run it executed twice just like if it inside a loop that managed to loop twice!

1 Like

You are correct, but it is expected behavior. I confirmed it with support a few months ago. (It seems to have started with plug-in API v3.)

-Steve

Thank you @sudsy,
This is straing!
Their response did not convince me because the element displayed duplicated on the page. So we need to adopt a method like the one I used to prevent this duplication!

I didn’t mean to suggest that you don’t have to handle that situation. You still have to code “defensively” to ensure that whatever you wish to happen only once in the update() method when your plugin first appears does in fact happen just once.

As others have stated (and as you seem to have already implemented), you’ll likely want to use a flag of some sort. I was merely stating that the dual invocation of the method when the element first appears on the page is in fact expected behavior.

3 Likes