Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Timer field resets

Posted on by Microsoft Employee

Hi experts, 

I have a single line of text field called new_timer and I have applied the following script to my form to start a stopwatch on the timer. 

//Initialize time attributes
var seconds = 0, minutes = 0, hours = 0,t;
function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
    }
    
	//creating time value in hh:mm:ss formate
    var tmp = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
	Xrm.Page.getAttribute("new_timer").setValue(tmp);

	//recursively calling timer
    timer();
}

//main timer method
function timer() {
    t = setTimeout(add, 1000);
}

function pause()
{
    clearTimeout(t);
}

function resume()   
{
    timer();
}


I have 2 buttons for pause and resume which I created on ribbon workbench and called pause and resume function, respectively. When I click pause, my timer pauses. When I click resume for the first time, my timer resets to 00:00:00. After that whenever I pause it resume, the timer stops and starts from where I left off.

Why is it resetting for the first time? Is there some error in my Script?

Thanks,

Jon

*This post is locked for comments

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Timer field resets

    Hi Jon,

     not that I see any errors.. Did you add that script as a web resource to the form, though? If you did, try removing it from the form resources. I'm guessing you are referencing it from the ribbon button action, too, so maybe what's happening is that it gets loaded twice and the variables get mixed up somehow..

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans