最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - the object doesn't accept property or method - Stack Overflow

programmeradmin3浏览0评论

I have this function in my scripts and Internet Explorer throws an error: "el objeto no acepta la propiedad o el metodo trunc" which means something like "the object doesn't accept property or method trunc"

function minutesToString(a){
  var hours = Math.trunc(a/60);
  var minutes = a % 60;
  return(hours +" hr "+ minutes + " m");
}

On chrome, firefox, etc. works perfectly.

I have this function in my scripts and Internet Explorer throws an error: "el objeto no acepta la propiedad o el metodo trunc" which means something like "the object doesn't accept property or method trunc"

function minutesToString(a){
  var hours = Math.trunc(a/60);
  var minutes = a % 60;
  return(hours +" hr "+ minutes + " m");
}

On chrome, firefox, etc. works perfectly.

Share Improve this question edited Jun 15, 2017 at 20:07 Quentin 944k132 gold badges1.3k silver badges1.4k bronze badges asked Jun 15, 2017 at 20:05 Sebastian BreitSebastian Breit 6,1591 gold badge36 silver badges54 bronze badges 2
  • IE does not support Math.trunc() – Ju66ernaut Commented Jun 15, 2017 at 20:08
  • @Quentin the question is obvious. Thanks for the info anyways – Sebastian Breit Commented Jun 15, 2017 at 20:14
Add a comment  | 

4 Answers 4

Reset to default 23

Add a polyfill for Math.trunc(). Include the following code somewhere before using Math.trunc()

Math.trunc = Math.trunc || function(x) {
  if (isNaN(x)) {
    return NaN;
  }
  if (x > 0) {
    return Math.floor(x);
  }
  return Math.ceil(x);
};

Simply use Math.floor instead or leave it away completely:

function minutesToString(a){
 var minutes = a % 60;
 var hours=(a-minutes)/60;
 return(hours +" hr "+ minutes + " m");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc#Browser_compatibility

This is the MDN, and you can look up JS functions here. On the right there is a browser compatibility section or at the bottom of the page. Considering IE sucks, it doesn't support this property (trunc) on the Math object.

parseInt() accepts numbers, is supported by IE and behaves just like Math.trunc

发布评论

评论列表(0)

  1. 暂无评论