javascript - Use SetInterval when the If condition is True -
is possible if value of $new changed var updatechart function reload again , again goes var check function , again have same condition on it.
var updateinterval = 3000; var updatechart = function() { var check = function() { if(<?php echo $new; ?>).change(function(){ setinterval(function(){updatechart()}, 1000); }); } dps.push(<?php echo $str; ?>); chart.render(); };
will appreciated thanks.
so want update chart value has been written on server-side, right? mentioned in comment, using php statements in js block not work php code run on page load , won't change afterwards. instead, should use two-step approach:
- use recurring ajax call poll changes
- create php page reports changes
js code (using jquery):
window.setinterval(function() { $.ajax('change-listener.php', { success: function(data) { if (data.changed) { for(var in data.newvalue) { dps.push(data.newvalue[i]); } chart.render(); } } }); }, 3000);
check rows have been written in given time span (assuming table right records has timestamp) , return corresponding values via json js script:
php code:
$query= 'select strvalue [yourtable] created > date_sub(now(),interval 3 second)'; $data= array(); if ($result = $mysqli->query($query)) { if (mysqli_num_rows($result)) { $data['changed']= true; while($obj = $result->fetch_object()){ $data['newvalues'][]= $obj->strvalue; } } else { $data['changed']= false; } } $result->close(); header('content-type: application/json'); print json_encode($data);
an alternate way store newly created values inside user's session:
php code, part 1 (inside script handles storing of new values):
session_start(); $_session['newvalues'][]= $str;
php code, part 2 (instead of polling database, poll session):
session_start(); if (isset($_session['newvalues'])) { $data['changed']= true; $data['newvalues']) $_session['newvalues']; unset($_session['newvalues']; } else { $data['changed']= false; }
the second approach pure single-user scenario: php script polls changes, changes returned once , list of changes reset never return same values twice.
Comments
Post a Comment