0

I am already using this piece of code to update a codebox in the page with the data retrieved using dyntask.php.

<script type="text/javascript">
if(typeof(EventSource)!=="undefined") {
    var eSource = new EventSource("dyntasks.php");
    eSource.onmessage = function(event, previous) { 
        document.getElementById("serverData").innerHTML = event.data; 
    };
}
</script>

The data collected using dyntask.php are extracted from a text file.

2018-05-10 14:02:01: starting task 99333
2018-05-10 14:02:03: task 99333 completed
2018-04-13 15:13:44: triggered tasks
2018-05-10 14:05:52: starting task 99334
2018-05-10 14:05:57: task 99334 completed
2018-05-10 14:11:01: starting hidden task 99335
2018-05-10 14:11:07: hidden task 99335 completed
updating...

The last row of the text file is always "updating...". What I need to do is to check continuously for the content of the row before the last one

  • the check should start every time the keyword "starting" is present in the row
  • when the keywork is found the script should start checking for the presence of the word "completed"
  • when the word "completed" is found the script should refresh the whole page

I don't know Javascript more than what is needed in order to create this little script. I have no clues about how to do it but I'm sure there are people out there able to add just TWO lines in the script to make it work as expected.

Can you please provide me with some help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

I'm not sure what format you need, and the regex could use some refining, but this should at least give you the ability to grab the value from the last line before the "updating..." text.

var text = `2018-05-10 14:02:01: starting task 99333
2018-05-10 14:02:03: task 99333 completed
2018-04-13 15:13:44: triggered tasks
2018-05-10 14:05:52: starting task 99334
2018-05-10 14:05:57: task 99334 completed
2018-05-10 14:11:01: starting hidden task 99335
2018-05-10 14:11:07: hidden task 99335 completed
updating...`;
var regex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}:)(.*?)\n(updating)+/;

var result = regex.exec(text);

document.write(result[1] + ' ' + result[2]);
Jay Jordan
  • 643
  • 4
  • 16
  • hi Jay, actually in the past i already managed to get the text selected but i couldn't replicate a match while declaring the condition in order to issue the reload statement. basically, i need a solution from the script to activate the reload when a string is found in a specific position. – Giovanni Recchia May 10 '18 at 15:16
  • Could you provide more details? – Jay Jordan May 10 '18 at 15:28
0

SOLUTION FOUND!

<script type="text/javascript">
if(typeof(EventSource)!=="undefined") {
    var eSource = new EventSource("dyntasks.php");
    eSource.onmessage = function(event, previous) { 
        document.getElementById("serverData").innerHTML = event.data; 
        var contents = event.data.split('<br/>') ;
        if(contents[6].indexOf('completed') !== -1 && contents[6] != localStorage.getItem('previous') && previous !== '') {
            window.location.href = '/index.php?id=taskmanager';
        };
        localStorage.setItem('previous', contents[6]);
    };
}
</script>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129