开发者

Simple JS timer using JS Fiddle

This may not be the place for this question, but here goes anyways.

I am trying to learn more about the JS timers and am using the JS Fiddle for this purpose. In my script, I am using a script that binds functionality to a few elements, but I need the JS Fiddle to not execute it until the page loads completely due to it needing all elements to be initialized and available (see my fiddle at: http://jsfid开发者_Python百科dle.net/radi8/W2b2M/4/). This fiddle is a VERY rough skeleton.

The format of the script is as follows: How can I make the JS Fiddle only load this after all other elements are finished?

$(document).ready(function() {
   var tmr = {
      init: function(){
      },
      somefunct1: function(){
      },
      somefunction2: function(){
      }
    };tmr.init();
});

Any help is appreciated.


document.ready() is the way to good. However, there are other issues with your code. This function is not defined correctly:

function stopTimer {
  clearInterval(timer);
}

Should be:

function stopTimer() {
  clearInterval(timer);
}

Also, startstop.value is not defined. What is startstop suppose to be?

Update

Your use of .val() is incorrect, and many other issues (fixed):

http://jsfiddle.net/W2b2M/17/


Check this Fiddle:

A simple Javascript Function to Set timer.

$(document).ready(function () {
    var input = 120;

    function calculateTime(timer) {
        var timer = timer;
        var mins = Math.floor(timer / 60);
        var secs = timer % 60;
        var time = (mins < 10 ? "0" : "") + mins + ":" + (secs < 10 ? "0" : "") + secs;
        return time;
    };

    setInterval(function () {
        data = calculateTime(input)
        if (input > 0)
        {
            $("#timer").text(data);
            input--;
        }
        else
        {
            $("#timer").text("Time Out, Njoy Coding in JavaScript")
        }
    }, 1000);
});

http://jsfiddle.net/MUMU1987/sUkjj/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜