我应该如何将 javascript 函数调用到 kohana 视图中?

发布于 2024-10-16 17:13:32 字数 1837 浏览 3 评论 0原文

我有一个简单的 kohana 视图,以及一个可以倒计时 30 分钟的 javascript。 我将javascript文件放入目录/media m中,其名称为countdown.js 。我的问题是:我怎样才能调用那个javascript?形成视图?或者从控制器中显示在视图中?我到底该如何在控制器或视图中处理 js 函数?

谢谢

倒计时js:

var javascript_countdown = function () { var time_left = 10; //倒计时秒数 var output_element_id = 'javascript_countdown_time'; var keep_counting = 1; var no_time_left_message = 'JavaScript 倒计时时间不多了!';

function countdown() {
    if(time_left < 2) {
        keep_counting = 0;
    }

    time_left = time_left - 1;
}

function add_leading_zero(n) {
    if(n.toString().length < 2) {
        return '0' + n;
    } else {
        return n;
    }
}

function format_output() {
    var hours, minutes, seconds;
    seconds = time_left % 60;
    minutes = Math.floor(time_left / 60) % 60;
    hours = Math.floor(time_left / 3600);

    seconds = add_leading_zero( seconds );
    minutes = add_leading_zero( minutes );
    hours = add_leading_zero( hours );

    return hours + ':' + minutes + ':' + seconds;
}

function show_time_left() {
    document.getElementById(output_element_id).innerHTML = format_output();//time_left;
}

function no_time_left() {
    document.getElementById(output_element_id).innerHTML = no_time_left_message;
}

return {
    count: function () {
        countdown();
        show_time_left();
    },
    timer: function () {
        javascript_countdown.count();

        if(keep_counting) {
            setTimeout("javascript_countdown.timer();", 1000);
        } else {
            no_time_left();
        }
    },
    init: function (t, element_id) {
        time_left = t;
        output_element_id = element_id;
        javascript_countdown.timer();
    }
};

}();

//倒计时时间(以秒为单位)和元素ID javascript_countdown.init(3673, 'javascript_countdown_time');

i am having a simple kohana view, and a javascript that makes a countdown of 30 minutes.
I hava put the javascript file into a directory /media m and its name is countdown.js
. My problem is: how can i call that javascript? Form the view? Or from the controller to be displayed in the view? and how exactly may i address in a controller or a view that js function or functions?

thank you

the countdown js:

var javascript_countdown = function () {
var time_left = 10; //number of seconds for countdown
var output_element_id = 'javascript_countdown_time';
var keep_counting = 1;
var no_time_left_message = 'No time left for JavaScript countdown!';

function countdown() {
    if(time_left < 2) {
        keep_counting = 0;
    }

    time_left = time_left - 1;
}

function add_leading_zero(n) {
    if(n.toString().length < 2) {
        return '0' + n;
    } else {
        return n;
    }
}

function format_output() {
    var hours, minutes, seconds;
    seconds = time_left % 60;
    minutes = Math.floor(time_left / 60) % 60;
    hours = Math.floor(time_left / 3600);

    seconds = add_leading_zero( seconds );
    minutes = add_leading_zero( minutes );
    hours = add_leading_zero( hours );

    return hours + ':' + minutes + ':' + seconds;
}

function show_time_left() {
    document.getElementById(output_element_id).innerHTML = format_output();//time_left;
}

function no_time_left() {
    document.getElementById(output_element_id).innerHTML = no_time_left_message;
}

return {
    count: function () {
        countdown();
        show_time_left();
    },
    timer: function () {
        javascript_countdown.count();

        if(keep_counting) {
            setTimeout("javascript_countdown.timer();", 1000);
        } else {
            no_time_left();
        }
    },
    init: function (t, element_id) {
        time_left = t;
        output_element_id = element_id;
        javascript_countdown.timer();
    }
};

}();

//time to countdown in seconds, and element ID
javascript_countdown.init(3673, 'javascript_countdown_time');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-10-23 17:13:32

您可以使用 kohana html 助手。在您的控制器函数或视图文件中,您可以这样设置:

echo html::script('media/countdown.js');

echo 语句放在 id 为“javascript_countdown_time”的 html 元素后面。如果可能的话,在结束 之前。

...
<div id="javascript_countdown_time"></div>
...
<script src="/media/countdown.js"></script>
</body>

You can use kohana html helpers. In your controller function or your view file, you can have it like this :

echo html::script('media/countdown.js');

Place the echo statement after the html element with id "javascript_countdown_time". Before the closing </body> if possible.

...
<div id="javascript_countdown_time"></div>
...
<script src="/media/countdown.js"></script>
</body>
指尖上得阳光 2024-10-23 17:13:32

你可以按照@erickb所说的去做,或者你可以在你的
view (php)

确保当页面显示在浏览器中时加载 countdown.js [ 使用 firebug 或Chrome 中的开发人员面板以验证它是否已加载。 ]

无论您想从哪里调用函数,只需调用它即可..让我们知道它是如何进行的..

You could do what @erickb said, or you could do this in your
view (php)

<script src="media/countdown.js"></script>

make sure that countdown.js is loaded when the page is displayed in your browser [ use firebug or developer panel in chrome to verify it was loaded. ]

Wherever you wanted to call the functions from , just call it.. Let us know how it went..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文