user8631713
user8631713

Reputation:

How to disable a button, if it is clicked once in a day and enable on next day using JavaScript and AngularJS 1?

My button should be disabled once I clicked in a day (i.e) only once in a day a user enter the details using submit button. Next day automatically button should be enabled.

Help me with JavaScript or AngularJS..

Upvotes: 3

Views: 763

Answers (4)

Faly
Faly

Reputation: 13346

You can use localStorage :

<button ng-disabled="!buttonIsActive()" ng-click="onClick()"></button>

$scope.buttonIsActive = function() {
    var now = new Date();
    var day = now.getDate();
    var lastClickDay = parseInt(localStorage.getItem("last-click-day"));        
    return day > lastClickDay;
}

$scope.onClick = function() {
    var now = new Date();
    var day = now.getDate();
    localStorage.setItem("last-click-day", day);
    // ... Do something else here
}

Upvotes: 0

Sangram Badi
Sangram Badi

Reputation: 4254

You can use timeout functionality. You can give specific time for timeout then after that timeout you can enable the button.

EX:

var theInterval = $interval(function(){
      //Enable button
   }.bind(this), time);  

Upvotes: 0

Marco
Marco

Reputation: 761

You can save the status of the click in the localstorage, but this solution is unsafe. You should add a server side control.

Upvotes: 0

pegla
pegla

Reputation: 1866

If you have backend you could maintain state in DB as Sajal said, but since you are asking about angularjs/javascript and frontend solution cookies (what Chris said) or localStorage is way to go, set the cookie and based on cookie you can set button each day to reset

more info on w3schools:

https://www.w3schools.com/js/js_cookies.asp https://www.w3schools.com/html/html5_webstorage.asp

Keep in mind cookies can be modified so DB is best way if you want fullproof solution.

Upvotes: 2

Related Questions