为什么我的函数不运行<object>

时间:2016-02-18 08:26:49

标签: javascript php html

index.php i中运行两次函数,其中包括chart.html

的index.php

chart_line($valuesNight);     //run function for the first time
chart_line($valuesEvening);   //run function for the second time
?>
<?php function chart_line($jsonDataSource){ ?>
                <div>
    <script type="text/javascript">

        var dataSource = JSON.parse('<?php echo $jsonDataSource ?>');
        var dValues = new Array;
        var dTimeDate = new Array;

        for(var o in dataSource) {
            dValues.push(dataSource[o]['dValue']);
            dTimeDate.push(dataSource[o]['dTimeDate']);
        }
        console.log('dValues', dValues);
        // store values
        sessionStorage.setItem('myValues', JSON.stringify(dValues));
        sessionStorage.setItem('myTime', JSON.stringify(dTimeDate));

    </script>     //include chart.html
            <object type="text/html" data="chart.html" width="100%"  height="420"></object>
                </div>
        <?php } ?>

chart_line($valuesNight);运行时console.log('dValues', dValues);会返回正确的值,但不会使用object运行此chart.html。然后chart_line($valuesEvening);运行,console dvalues返回晚间数据。之后chart.html开始两次,两次都给我最新(晚上)数据。我该如何解决这个问题?

chart.html

<html>
    <body>
        <script type="text/javascript">

// retrieve value
var myTime = JSON.parse(sessionStorage.getItem('myTime'));
var myValuesStr = JSON.parse(sessionStorage.getItem('myValues'));

var myValues = myValuesStr.map(function(item) {
    return parseInt(item, 10);
});

console.log('myTime', myTime);
console.log('myValues', myValues);

$(function () {
    $('#container').highcharts({
        //  ...
    });
});
        </script>

<script src="js/charts.js"></script>
<script src="js/modules/exporting.src.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

控制台按此顺序执行:

console.log('dValues', dValues); // from index.php  - night values
    // HERE SHOUD EXECUTE CONSOLE FROM CHART.HTML
console.log('dValues', dValues); // from index.php  - evening values

console.log('myTime', myTime);      // from chart.html  - evening values
console.log('myValues', myValues);  // from chart.html  - evening values
console.log('myTime', myTime);      // from chart.html  - evening values
console.log('myValues', myValues);  // from chart.html  - evening values

1 个答案:

答案 0 :(得分:2)

临时解决方案,但它有效:

//for valuesNight
$(function () {
    $('#container1').
//...
<div id="container1">

//for valuesEvening
$(function () {
    $('#container2').
//...
<div id="container2">