0

i have created a circle with jquery-knob. <input type="text" value=0 class="timecycle"/>. Now if I want to change the value, right after I created the circle, my circle is:

$(".timecycle").knob({ min: 0,max: 50,width: 292,height: 292,fgColor: '#00BB00',bgColor: '#eeeeee',thickness: '.05',readOnly: true});
$(".timecycle").val(25).trigger('change');

Well, but I want to change the value dynamically via ajax-Request. But now if I try to change the value within my ajax-request, the circle is not filling (but the value is updating; if i set value to "2", the value will be "2" (I debugged it already) but my circle remains empty). How can i change my value correctly with ajax, so that my circle is changing. Here is my current code:

$(document).ready(function(){
    setInterval(function(){
        $.ajax({
            type: "POST",
            url: "assets/php/ajax.php",
            data: "do=check",
            success: function(e) {
                var json = JSON.parse(e);
                $('.timecycle').val(json[0]).trigger('change');
                console.log(json); // json[0] = 2 and value the value was successfully set, but is not triggering
            }
        });
    }, 3000);
});
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • There is no difference between an Ajax success callback and any other code. However you should not call `JSON.parse` yourself, jQuery does that for you. – Tomalak Mar 04 '16 at 17:02
  • But if I remove the JSON.parse, its displaying chars of the string and no numbers. – Titus Andronicus Mar 04 '16 at 17:16
  • Does your php page send a `Content-Type: application/json` header? If not, it should, because that's how jQuery figures out that the data is JSON that needs to be decoded. – Tomalak Mar 04 '16 at 17:19
  • I did not even know this, thank you – Titus Andronicus Mar 04 '16 at 17:21
  • No problem. Other than that, if you can run `$('.timecycle').val(2).trigger('change');` in the browser console and the correct thing happens, then the correct thing will definitely also happen in the Ajax success callback. (Cross check: if you put `$('.timecycle').val(2).trigger('change');` in the success callback and it works, then there's something wrong with your JSON.) – Tomalak Mar 04 '16 at 17:25
  • No, its not working if I run `$('.timecycle').val(2).trigger('change');` but all other actions (like hide the whole element) are working. From default the value of my circle is "0", but my json[0] is setting it to "2", it is printing my values correctly. But the `.trigger('change')` is not working. So it is settings val to 2 but is not triggering the event – Titus Andronicus Mar 04 '16 at 17:32
  • This is most definitely triggering the event. Maybe your assumption that the jquery-knob is listening to the `change` is wrong? (I haven't checked its documentation, have you?) – Tomalak Mar 04 '16 at 17:36
  • But it is triggering the event, if i trigger it directly after the initialization: `$(".timecycle").knob({ min: 0,max: 50,width: 292,height: 292,fgColor: '#00BB00',bgColor: '#eeeeee',thickness: '.05',readOnly: true});` `$('.timecycle').val(2).trigger('change');`. I would not know any other way to change my circle – Titus Andronicus Mar 04 '16 at 17:48
  • It's impossible to say. Make a jsFiddle that reproduces the behaviour (that would have been useful right from the start). – Tomalak Mar 04 '16 at 17:53
  • If i do it on jsFiddle it works, but in my script its not working, now i get the error: Uncaught TypeError: $(...).knob is not a function. But knob is included, same file like the file i used in jsFiddle. – Titus Andronicus Mar 04 '16 at 20:51
  • Okay, dont have included knob correctly, but i still is not doing anything – Titus Andronicus Mar 04 '16 at 21:30

0 Answers0