• Resolved 100IT

    (@100it)


    this piece of code works fine on a html page. (put in the header)

    var startstop = new (function() {
    
        console.log('startstop called');
    
        var $stopwatch, // Stopwatch element on the page
            incrementTime = 70, // Timer speed in milliseconds
            currentTime = 0, // Current time in hundredths of a second
            updateTimer = function() {
    
                $stopwatch.val(formatTime(currentTime));
                currentTime += incrementTime / 10;
            },
            init = function() {
    
                $stopwatch = $('.stopwatch');
                startstop.Timer = $.timer(updateTimer, incrementTime, true);
                startstop.Timer.stop();
            };
            this.resetStopwatch = function() {
                currentTime = 0;
                this.Timer.stop().once();
            };
       $(init);
    });

    When I try to access it on a WordPress page where this javascript is loaded in the header using a theme function console shows $(init) not a function.

    What is wrong with this way of object declaration in WordPress?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter 100IT

    (@100it)

    solved, moved variable outside function to make it global and added a onready jquery function:

    var startstop;
    
    (function ($) {
        jQuery(document).ready(function () {
    
    rest of code....
    Thread Starter 100IT

    (@100it)

    solved

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘javascript object works on a html page but can not be loaded in wordpress’ is closed to new replies.