Counting how many times a function was executed

Counting how many times a function was executed
0

#1

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);
    }

#2

@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.


#3

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


#4

And this:

count++;

This will also increment count. How is this function called? Can you provide more code? Where are you printing count in your code?


#5

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();
}


#6

I assume the count variable is local to the PanelButton? You did not post the PanelButton code.


#7

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


#8

So where does count get displayed and can you show us exactly how you were trying to use count += 1 in your code which did not work?


#9

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.


#10
count += count;

This will always make count 0. 0 + 0 = 0. You did not use what was suggested to you which was:

count += 1;

or

count++;

The other approach would be to increment count in:

LabelWithText.text = "output:" + ++count;

and not use the other ways suggested.


#11

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 @RandellDawson and @TomerPacific for comments.


#12

When I referenced the above, I meant for it to be used like:

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

Neither @TomerPacific or I wrote:

count = count++;

#13

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