0

I have a background Task looping looks like this:

Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
     if (hourNow >= cashCutOff_Start && hourNow <= cashCutOff_End - 1) {
//Run the code once
}
}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
fiveSecondsWonder.play();

This code does make loop every one second. But I want to make a single line of code executable once this code run.

John Smith
  • 21
  • 5
  • Either create 2 separate `Timeline` or use a boolean variable to test inside to know if this part of code has run already – Kaddath Mar 06 '19 at 10:00
  • 1
    Unrelated to your problem: please learn java naming conventions and stick to them. Related: it's rather unclear what you are after - please provide a [mcve] that demonstrates the problem and how it doesn't meet your requirements. – kleopatra Mar 06 '19 at 10:07
  • @kleopatra where is he not following naming conventions? – Stultuske Mar 06 '19 at 10:09
  • 2
    the underscores should be camel-case – kleopatra Mar 06 '19 at 10:11
  • underscores aren't the prettiest sight, but they are allowed (as per an interpretation of the conventions Oracle has https://www.oracle.com/technetwork/java/codeconventions-135099.html ) – Stultuske Mar 06 '19 at 10:27
  • 1
    hehe, this variable name sure looks familiar: https://stackoverflow.com/questions/9966136/javafx-periodic-background-task/9966213#9966213 :D – Sergey Grinev Mar 06 '19 at 11:07
  • hahahahaha true – John Smith Mar 06 '19 at 11:48
  • @Stultuske don't see any statement in that document that suggests using underscores (except for constants which are all-caps, so have no other means to seperate words) .. obviously they are allowed by the language (otherwise the compiler would complain) but violating naming conventions ... repeating to drive it home – kleopatra Mar 12 '19 at 10:08
  • @kleopatra all the guidelines state is that variable names shouldn't START with an underscore: "Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed." The problem with sentences like this in guidelines, is that they are a bit ambiguous and open for interpretation. – Stultuske Mar 12 '19 at 10:10
  • @Stultuske no there is nothing open - they state clearly what you should do for separating out the words, and that's camel-case, nothing else. By convention, we should follow the affirmative sentences in conventions :) – kleopatra Mar 12 '19 at 10:13

2 Answers2

1

Just remove fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
0

Simple solution in two steps.

Create a boolean variable:

private boolean hasRun = false;

Add if statement in your Timeline:

Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
    //check if code has run before
    if(!hasRun){
        //this will run only once
        //by setting hasRun = true;
        hasRun=true;
        //add your code here...
    }
    //this code will run in every KeyFrame
    //add your code here...
    }));
    fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
    fiveSecondsWonder.play();
amarildo.xyz
  • 932
  • 1
  • 13
  • 21