问题
I'm fooling around with a codeigniter 2.X app and I'm trying to use the following Jquery code to update a number every 10 seconds in my view.
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_score').load('<?php echo $FighterOneScore; ?>').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>
And this is my div tag in my view
<div id="load_score"> </div>
UPDATE
Here is my controller code
public function left()
{
$this->load->model('points');
$data['ScoreOne'] = $this->points->get_ScoreOne();
echo $data['ScoreOne'];
}
public function right()
{
$this->load->model('points');
$data['ScoreTwo'] = $this->points->get_ScoreTwo();
echo $data['ScoreTwo'];
}
}
?>
Here is the Javascript I have in My VIEW:
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$.get("<?php echo base_url(); ?>response/left",
function(data){ $("#load_score").html($data); } // not sure about fadeIn here!
);
}, 10000); // refresh every 10000 milliseconds
</script>
When I load my view in the browser - The page shows nothing.
回答1:
How you can do this is,
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$.get("<?php echo base_url(); ?>sampleController/sampleData",
function(data){ $("#load_score").html($data); } // not sure about fadeIn here!
);
}, 10000); // refresh every 10000 milliseconds
</script>
And, in php, create a controller sampleController
with a method sampleData
that will do:
echo $FighterOneScore;
回答2:
In your load call, you're not actually calling a page. You're echoing out some value from php.
You need the parameter to load to be a URL to a controller that echoes out the score.
来源:https://stackoverflow.com/questions/11869834/using-jquery-in-codeigniter-app-to-update-div-every-10-seconds