Counting how many times a function was executed

count variable is not incrementing and keeps printing out 0, even if _showHello() is executed. Am I doing something incorrectly?

var count = 0;
function _showHello() {
	count = count++;
    if (!LabelWithText) {
        LabelWithText = new LabelWidget();
	LabelWithText.text = "output:" + parseInt(count);
        Main.uiGroup.add_actor(LabelWithText);
    }

@boqsc,

You have only provided a small snippet of your code so I am going to take several assumptions about when and how the function is run.

You have to pay attention to the next line and its meaning:

count = count++;

The command count++ only increments the value of count after the execution of this line. Meaning, count will be zero at the end of the execution of this line of code. Only after that, does the incremental effect take place.

Furthermore, there is no logic in writing that line of code. Why not write:

count++ or 
count +=1 or 
count = count + 1

Apart from that, make sure that the count variable is not being overridden anywhere else in your code.

Prints out 0, looked kind of strange, I tried it earlier and this is the main reason I started this thread.

Prints out 1, seems to be correct way.

count = ++count;

Prints out 1, correct way aswell.

@TomerPacific I might keep using ++count and count = count + 1 mainly from now on.
Instead of count++ and count += 1

PanelButton._showHello(); is how it is called.

This is the whole script that imports PanelButton.js into Extension.js using Gnome Seed importer. And uses _showHello() function when Gnome Extension that I’m writting is enabled.

Now this should be confusing, as I’m programming for the Gnome environment and not Chrome or Firefox browser which don’t need such thing. Not sure if that is even important to explain everything and bring in even more confusion.

Don’t look below at the script, as you might get heart-attack.
Although it looks kind of simple, as I look at it now.

"use strict"
//Importing Native Modules 
const ExtensionUtils = imports.misc.extensionUtils;
const ME = ExtensionUtils.getCurrentExtension();

//Importing from current extension's folder.
const PanelButton = ME.imports.PanelButton;

//Importing Gnome UI
const Main = imports.ui.main;


class Extension {
    constructor() {
	
	// Create new Button from . /PanelButton.js
        this.button = new PanelButton.Button;
    }


    enable() {

	// Assign the Button to the rightBox of Gnome-Top-Panel
        Main.panel._rightBox.insert_child_at_index(this.button, 0);

	// Show Hello Automatically when Extension is enabled 
        PanelButton._showHello();
	
    }


    disable() {
        Main.panel._rightBox.remove_child(this.button);
    }

}

function init() {
    return new Extension();
}

var count belongs to PanelButton, which as you have seen has ._showHello()

This is where it gets displayed:

This is the whole function inside PanelButton.js:

var count = 0;
function _showHello() {
	count += count;
    if (!LabelWithText) {
        LabelWithText = new LabelWidget();
	LabelWithText.text = "output:" + parseInt(count);
        Main.uiGroup.add_actor(LabelWithText);
    }

Main.uiGroup.add_actor(LabelWithText); This is somewhat similar to alert(); that could be seen in browsers like Firefox and Chrome that produces popup seen in the picture I made.

And this is how it is called: PanelButton._showHello(); from any script.

Seems to work as expected, I’m not sure what I’ve done before that lead me to thinking that 0 as an answer was illogical.
Might be my mistakes as you pointed out.
I’m getting too tired I guess, will try to re-read this thread tomorrow.

BTW:
count = count++; actually works correctly and should output 0.
As assignment is done before increment.

I was just so silly to not notice that. Might aswell tired. #AlwaysReReadYourCodeInEnglish

Thanks @camperextraordinaire and @TomerPacific for comments.

Yup, and I understood it the other way. :laughing:
Next time just edit the code as you did right now.