7

I have a text file located on the server that contains a list of events with their target time. It looks something like the following:

2012/02/11-10:00:00 EventStart Red
2012/02/11-10:10:00 EventStop Green
...

What I need to do is somehow read the text file and based on the current time select the next upcoming event and assign each element for that event to a variable. So for example if the time is currently 2012.02.11-10:08:00, javascript variables time = '2012/02/11-10:10:00'; title = 'EventStop'; color = 'Green'; would be created.

I'm able to read the text file with:

jQuery.get('schedule.txt',function(data){
  alert(data);
});

Just don't know where to go from there, or if that's even the best way to start. I do have control over the formatting of the text file, so modifying that is an option.

3
  • 5
    It looks like a something that should be done on the server, not on the client. Commented Feb 14, 2012 at 21:41
  • @gdoron I agree as well, however in this particular case it needs to be done on the client. Commented Feb 14, 2012 at 21:48
  • 1
    @gdoron. I agree too. ;-) lol Commented Feb 14, 2012 at 21:50

3 Answers 3

9

You say you can modify the file's contents, so I suggest converting it to JSON (and separating the date/time).

[{"date": "2012/02/11", "time": "10:00:00", "title": "EventStart", "color": "Red"}, {"date": "2012/02/11", "time": "10:10:00", "title": "EventStop", "color": "Green"}]

Then you can use getJSON to get/parse it.

jQuery.getJSON('schedule.txt',function(data){
    // data is an array of objects
    $.each(data, function(){
       console.log(this.title); // log each title
    });
});

From here, you can read the times and figure out which one is the latest. Something like this should work:

if(Date.now() <= Date.parse(this.date+' '+this.time))

So, putting it all together:

jQuery.getJSON('schedule.txt',function(data){
    var matchedSchedule = {};
    // data is an array of objects
    $.each(data, function(){
       if(Date.now() <= Date.parse(this.date+' '+this.time)){
          matchedSchedule = this;  // If time matches, set a variable to this object
          return false; // break the loop
       }
    });
    console.log(matchedSchedule.title);
    // to set a "global" variable, add it to `window`
    window.eventTitle = matchedSchedule.title;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. Re-formatted the text file to JSON as suggested and am able to run the script above. However I'm not getting the last part. So how can I stop on the first match (for simplicity sake, current time is less than this.time) and assign a variable for each element (date, time, title, color)?
If you return false; in the function passed to $.each, it'll stop the loop. If the time matches, then you can set the variables, and return false;.
That did the trick. Had to use jQuery.ajax with async set to false instead of jQuery.getJSON else I could not get any of the set variables out (even using window.varname...).
5

Without changing your text file

$.get('sometext.txt',function(data){
    var perLine=data.split('\n');
    var myVars=[];
    for(i=0;i<perLine.length;i++)
    {
    var line=perLine[i].split(' ');
    myVars[i]={
        'time':line[0],
        'event':line[1],
        'color':line[2]
        }
    }
    console.log(myVars);
    console.log(myVars[0].time);
    console.log(myVars[0].event);
    console.log(myVars[0].color);
});

Comments

0

Sounds like you've already done the hard part. Now just parse the data that is returned.

jQuery.get('schedule.txt',function(data){
  yourParseFunction(data);
});

4 Comments

So with this particular route, how would I create global variables from the function? When I try to call variables created within that function they are not defined.
does @Rocket's answer help or do you still need an answer?
@Rocket's answer helps, but still curious on how to 'export' a variable from within the above function.
if you want to make it global then add it to the window namespace. You do that by either omitting the var keyword or directly with window.yay = 'yay'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.