简体   繁体   English

多次触发击倒点击事件

[英]Triggering knockout click event multiple times

I am working on a single page phonegap application which uses jquerymobile and knockout. 我正在使用jquerymobile和knockout的单页phonegap应用程序。

Inside the JqueryMobile pageInit I have defined the sampleViewModel() as follows. 在JqueryMobile页面内部我已经定义了sampleViewModel(),如下所示。

function sampleViewModel()
{
var self=this;
var hourvalue=14;
self.sample_data= ko.observableArray([
            { title: "hello", hour:hourvalue }    
        ]);
}

//Variable declaration for the above view model //上述视图模型的变量声明

var sample_datavar = { viewModel: new sampleViewModel() };

I want to change the hourvalue based on the current system hour value. 我想根据当前系统小时值更改小时值。 Any better solutions for the same is appreciated. 任何更好的解决方案是值得赞赏的。

Am trying out the following logic wherein I will get the system hour every second and pass it to the datasource via that variable assignment. 我正在尝试以下逻辑,其中我将每秒获得系统小时并通过该变量赋值将其传递给数据源。

setInterval(function () {
        var now = new Date();
        var hour = now.getHours();
        if (hour.toString().length == 1) {
            var hour = '0' + hour;
        }

sample_datavar.viewModel.sample_data([
            { title: "hello", hour:hourvalue }    
        ]);
  },1000);

Problem is that I can define this only inside a click event which will be inside the pageinit call function. 问题是我只能在clickinit调用函数内部的click事件中定义它。

1)Is there a way to make the knockout click function to trigger inside the pageshow event of jquery mobile? 1)有没有办法让淘汰赛点击功能在jquery mobile的pageshow事件中触发?

2)If am complicating things?..Is there a better way of doing that hourvalue changes? 2)如果使事情复杂化?有没有更好的方法来进行小时值变化?

First of all, please don't check every second if the hour has changed, it hurts my eyes :) --unless the time of the system is supposed to change unexpectedly. 首先,如果时间已经改变,请不要每秒检查一次,它会伤害我的眼睛:) - 无论系统的时间是否应该意外改变。

Then why do you need a click event? 那为什么你需要点击事件? Why don't you just start to check at loading and then check again and again? 你为什么不开始检查加载,然后反复检查?

You can calculate the seconds before the next check (you could event set the new hour without checking): 您可以计算下次检查前的秒数(您可以在不检查的情况下设置新的小时):

function sampleViewModel()
{
    var self=this;
    var hourvalue=14;
    self.sample_data= ko.observableArray([
            { title: "hello", hour:hourvalue }    
        ]);
}

var viewModel = new sampleViewModel();
ko.applyBindings(viewModel);

function checkHour() {
        var now = new Date();
        var hour = now.getHours();
        var secondsBeforeNextCheck = 3600 - now.getMinutes() * 60 - now.getSeconds() +1;
        document.getElementById('calculatedSeconds').innerHTML = secondsBeforeNextCheck;
        if (hour.toString().length == 1) {
            var hour = '0' + hour;
        }

        viewModel.sample_data([
            { title: "hello", hour:hour }    
        ]);
        setTimeout(checkHour, secondsBeforeNextCheck * 1000);
}

checkHour();

http://jsfiddle.net/7nBNW/2/ http://jsfiddle.net/7nBNW/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM