如果警报被注释掉,此JavaScript代码将无法正常执行

This JavaScript code will not execute properly if the alert is commented out.

本文关键字:常执行 执行 代码 注释 JavaScript 如果      更新时间:2023-09-26

我正在尝试为web应用程序编写一个简单的JavaScript页面计时器。计时器将记录按下按钮的时间(当用户进入页面时),并再次记录按下按钮(当用户离开页面时)的时间。两次时间的差值将给出用户在页面上花费的时间。如果他们在页面上停留超过2秒,则会将秒数记录到手持的本地数据库中。

我遇到的问题是,如果starttime函数中的alert被注释掉,下面的代码将不起作用。

$(document).ready(function() {
    $('div.m1-root').click(function () {
      //simulate the pagetransition events in mobile framework
        pagename= $(this).attr('id'); 
        starttime();
    //alert("You are leaving'n" + pagename + "'n you were here for 'n" + time.toFixed(0) + " seconds"); // This alert will fire but only record the proper time if the next alert is fired
    });
}); 
function starttime()
{
//create a starting timestamp, number of milliseconds since midnight Jan 1, 1970
start = new Date().getTime();
//alert(+ start); // if this alert is commented out the time is not started
    pagetime();
}
function pagetime(pagename){
time = (new Date().getTime() - start) / 1000;
//alert("you have been on " + pagename + " for 'n" + time.toFixed(0) + " seconds");
//before we transition, log previous page and time
//if time is greater than 3 seconds we log it (I changed it to -1 so that I would see any figure while testing)
if (time >-1)
{
//DataBase update
db.transaction(function(tx) {tx.executeSql('INSERT INTO CBNapp_Usage VALUES (time)',function(tx,result) {},function(tx,Error) {});});   
//alert("You spent " + time.toFixed(0) + " seconds on " + pagename);
}
}

我看过SO和网络,也遇到过类似的情况,但我似乎无法在这里找到任何这些想法。我知道这可能是一个时间问题,并尝试了setTimeout或return true来延迟事情,但它不起作用。

有人能看一下代码并建议我如何使其正常工作吗?

这是我第一个从头开始的JS。请提供具体的例子,这样我就可以照着做了。数据库也没有记录,但我稍后会处理。如果你对如何改进这一点有任何其他具体建议,我将欢迎他们。

谢谢你花时间帮忙。

Ted

看起来您的所有代码都在按钮点击处理程序中。只有当您有警报时,它才会显示为工作,因为警报会造成延迟的外观。

您需要将starttime()放在点击处理程序之外,它不应该调用pagetime()。

$(document).ready(function () {
    starttime(); // this will run as soon as the page loads
    $('div.m1-root').click(function () {
        pagetime();
        // leave the page
    });
});
function starttime() {
    start = new Date().getTime(); // start is global
}
function pagetime() {
    var time = (new Date().getTime() - start) / 1000;
    // do something with time, like logging it to your db
}

在您的代码中:

  pagename= $(this).attr('id');

当代码执行时,上面将创建一个名为pagename的全局变量。如果你想这样做,你应该在适当的范围(即全局)中声明它。然而,通常认为最好将其存储为对象属性或闭包,并避免不必要的全局变量。

此外,它的使用效率要高得多:

  pagename = this.id;

线路:

  starttime(); 

调用starttime函数。。。

function starttime() {
  //create a starting timestamp...
  start = new Date().getTime();

同样,请确保在适当的范围内声明变量,或者使用其他策略共享值。此外,您可以将start保留为Date对象(即不要调用getTime)。

  //alert(+ start); // if this alert is commented out the time is not started

这通常表明alter创建的暂停正在"修复"您的问题,因此这与时间有关。

  pagetime();

现在它调用页面时间

function pagetime(pagename) {
  time = (new Date().getTime() - start) / 1000;

又是一个隐含的全球性。

在这里,您在设置开始后立即调用页面时间,很可能时钟没有移动。在某些浏览器中,计时器分辨率约为15ms,因此,除非CPU非常忙于做其他事情,或者您有机会达到15ms的边界(极不可能),否则new Date()将具有与start完全相同的值。

您也可以执行以下任务:

  time = (new Date()) - start;

但是时间几乎肯定是零,bhamlin对此有解决方案。