1

I am getting this

Uncaught SyntaxError: Unexpected identifier error

, Why ? I think i have used the syntax properly ?

// I've revised it
var json;
json = '{"1438905600":0,"1438992000":0}';
setTimeout( $('#example-heatmap').each(function() {
    if( json != null ){
    var obj = $.parseJSON(json);
    var now = new Date();
    new CalHeatMap().init({
      data: obj,
      start: new Date(2016, 0),
      cellSize: 11,
      range: 9,
      domain: 'month',
      domainLabelFormat: '%Y-%m',
      itemSelector: '#example-heatmap',
      legend: [1, 3, 5, 7],
      legendColors: {
        min: '#efefef',
        max: 'steelblue',
        empty: '#efefef'
      },
          tooltip: true
      //start: new Date(now.getFullYear(), now.getMonth() - 9)
    });
    } else {
        setTimeout( arguments.callee, 100 );
    }
}));
3
  • You have this in a script tag in your HTML, right? Not in a separate JavaScript file? Commented Aug 7, 2016 at 8:40
  • 4
    Surely the error points out which line the error is on - because the code you posted does not produce that error at all Commented Aug 7, 2016 at 8:45
  • Sorry, I've revised it. Commented Aug 7, 2016 at 11:34

2 Answers 2

1

I think your .replace() is not correct.

var tmp = content.replace('<![CDATA[').replace(']]>');

.replace() expects searchvalue and newvalue as given on W3Schools

string.replace(searchvalue,newvalue) 
Sign up to request clarification or add additional context in comments.

4 Comments

That is probably a problem, but not having the second argument would not lead to a syntax error.
@JLRishe yes ... it won't but I can't find any other problem with that code.
Sorry, I've revised it.
@syui could you check the console for line no, where the error is occuring
0

You are passing a call to jQuery#each into setTimeout, but setTimeout expects a function or a string containing a JavaScript code block. jQuery#each returns neither.

The value returned is being converted to the string [object Object] and this is what's causing the syntax error.

To fix this, create a lambda function to contain your call to jQuery#each:

setTimeout(function() {
    $('#example-heatmap').each(function() {
        if (json != null) {
            var obj = $.parseJSON(json);
            var now = new Date();
            new CalHeatMap().init({
                data: obj,
                start: new Date(2016, 0),
                cellSize: 11,
                range: 9,
                domain: 'month',
                domainLabelFormat: '%Y-%m',
                itemSelector: '#example-heatmap',
                legend: [1, 3, 5, 7],
                legendColors: {
                    min: '#efefef',
                    max: 'steelblue',
                    empty: '#efefef'
                },
                tooltip: true
                    //start: new Date(now.getFullYear(), now.getMonth() - 9)
            });
        } else {
            setTimeout(arguments.callee, 100);
        }
    });
});

I must say, however, that what you are doing looks very hacky. You're using arguments.callee, which is now deprecated, apparently in some attempt to "wait" on an asynchronous operation. You should instead handle the asynchrony properly, using callbacks or promises.

1 Comment

Thank you very much for the detailed explanations.

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.